-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsqlite2yml.pl
More file actions
executable file
·73 lines (62 loc) · 1.53 KB
/
sqlite2yml.pl
File metadata and controls
executable file
·73 lines (62 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use Data::Dumper;
use Carp;
use Getopt::Std;
our $uniq = 1;
our %opt;
getopts('d:f:hS:', \%opt)
or usage();
$opt{h} && usage();
$opt{d} || usage();
#$opt{f} || usage();
#print Dumper(\%opt);
my $mydb = DBI->connect("DBI:SQLite:dbname=$opt{d}",
'','',
{RaiseError=>1, PrintError=>1}
);
# SQL below from http://stackoverflow.com/questions/82875/how-do-i-list-the-tables-in-a-sqlite-database-file
my $tables = $mydb->selectcol_arrayref(q{
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1
});
#print Dumper(\$tables);
for my $table (@$tables) {
my $cols = $mydb->selectall_hashref(qq{ PRAGMA table_info("$table") }, 'name');
my $vstr = '?,' x scalar keys %$cols;
$vstr =~ s/,$//;
my @cols = sort keys %$cols;
my $cstr = join(',', map { qq|"$_"| } @cols);
#print Dumper($vstr);
my $count = $mydb->selectrow_array(qq{ SELECT COUNT(*) FROM "$table" });
if ($count) {
print "\n$table:\n";
}
my $stm = $mydb->prepare(qq{ SELECT * FROM "$table" ORDER BY ID });
$stm->execute();
while (my $row = $stm->fetchrow_hashref()) {
ydump($table, $row, 4);
}
}
sub usage {
print "$0 -d DATABASE.db -f OUTFILE\n";
die;
}
sub ydump {
my ($table, $obj, $indent) = @_;
our $uniq;
my $pfx = ' ' x $indent;
print "$pfx\L$table$uniq:\n";
++$uniq;
for my $k (sort keys %$obj) {
my $v = $obj->{$k} || '';
print "$pfx$pfx$k: $v\n";
}
print "\n";
}