Monday, November 10, 2014

Dump the contents in a GnuCash file

Here's the perl code to dump the contents in a GnuCash file:

#!/usr/bin/env perl
# regc.pl - Reverse Engineer GnuCash

use strict;
use warnings;
use 5.10.0;
use DBI;
use Data::Dumper;

# Global Variables
my $gc_file = 'empty.gnucash';
my $dbh = DBI->connect("dbi:SQLite:dbname=$gc_file","","");

my $t = tables($gc_file);
say table_contents($t);

sub tables {
    my $file = shift;
    my $tables = [];

    my $res = `echo .schema | sqlite3 $file`;
    foreach my $t (split /;/, $res) {
        next unless $t =~ /CREATE TABLE (.*) \(/;
        push @{$tables}, $1;
    }
   
    return $tables;
}

# Given a list of tables, dump the contents of the tables to screen
sub table_contents {
    my $tables = shift;
    my $content = '';

    foreach my $t (sort @{$tables}) {
        $content .= "** $t **\n";
        my $sql = "SELECT * from $t;";
        my $res = `echo "$sql" | sqlite3 $gc_file`;
        $content .= $res . "\n";
    }
    return $content;
}

An empty GnuCash file has the following contents. Where double asterisks shows the table name:
** accounts **
1d61881fa764645df6f203834c0c3030|Root Account|ROOT||0|0||||0|0
d6b5387e81f169a5d4fbabca7130b9f4|Template Root|ROOT||0|0||||0|0

** billterms **

** books **
4e9e052c3dfc6c82d3f24f654ff80229|1d61881fa764645df6f203834c0c3030|d6b5387e81f169a5d4fbabca7130b9f4

** budget_amounts **

** budgets **

** commodities **

** customers **

** employees **

** entries **

** gnclock **
i3|6315

** invoices **

** jobs **

** lots **

** orders **

** prices **

** recurrences **

** schedxactions **

** slots **

** splits **

** taxtable_entries **

** taxtables **

** transactions **

** vendors **

** versions **
Gnucash|2041300
Gnucash-Resave|19920
accounts|1
books|1
budgets|1
budget_amounts|1
commodities|1
lots|2
prices|2
schedxactions|1
transactions|3
splits|4
billterms|2
customers|2
employees|2
entries|3
invoices|3
jobs|1
orders|1
taxtables|2
taxtable_entries|3
vendors|1
recurrences|2
slots|3
As you can see, this is Gnucash v2.4.13.

No comments:

Post a Comment