#!/usr/bin/perl -- use strict; use warnings; use DBI; use FindBin; use autodie 1.999; chdir $FindBin::Bin; eval { # create/init test.sqlite #http://svn.ali.as/cpan/trunk/SQLite-Default/share/default.sql my $dbh = DBI->connect( 'dbi:SQLite:dbname=test.sqlite', undef, undef, { RaiseError => 1, PrintError => 1, } ); $dbh->do($_) for q~ create table foo ( id integer not null primary key, str varchar(255) null );~, q~ create table foobar ( id integer not null primary key, str varchar(255) null );~, q~insert into foo (id, str) values ( 1, 'value foo' );~, q~insert into foobar (id, str) values ( 1, 'value foobar' );~; }; #http://cpansearch.perl.org/src/ADAMK/SQLite-Archive-0.02/lib/SQLite/A +rchive.pm # ??? # SQLite::Archive->new(...)->create_dir('fromthisfile.sqlite','tothisd +ir'); create_dir( { csv_attr => ''}, 'test.sqlite', 'outdir' ); sub create_dir { use DBI; use DBD::SQLite; use Text::CSV_XS; # prereq for SQLite::Archive my ( $self, $file, $outdir ) = @_; my $dbh = DBI->connect("dbi:SQLite:dbname=$file"); my $tables = $dbh->selectall_arrayref( q~ SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name; ~ ); use autodie; open OUT, '>', "$outdir/create.sql"; for my $ix ( 0 .. $#$tables ) { print OUT $tables->[$ix][1], ";\n"; } close OUT; for my $ix ( 0 .. $#$tables ) { my $table = $tables->[$ix][0]; open OUT, '>', "$outdir/$table.csv"; my $sth = $dbh->prepare( "select * from " . $dbh->quote($table +) ); if ( $sth->execute ) { my $csv = Text::CSV_XS->new( $self->{csv_attr} ); while ( my $ary_ref = $sth->fetchrow_arrayref ) { ## warn Dumper($ary_ref); $csv->print( \*OUT, $ary_ref ); } } close OUT; } ## use Data::Dumper; ## print Data::Dumper->new( [ $tables, $tables_sql ] )->Indent(1)-> +Dump; }
In reply to Re^2: making an SQLite::Archive?
by Anonymous Monk
in thread making an SQLite::Archive?
by azadian
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |