2

How to attach several sqlite databases into a single $dbh in Perl? In the command line I can do attach in the interactive sqlite3 rpel, how about with dbd-sqlite in Perl?

Sorry if this has been already answer here, perlmonks or similar but was not able to find a proper answer.

Pablo Marin-Garcia
  • 4,151
  • 2
  • 32
  • 50
  • Why do you think you can load multiple databases at once with one instance of dbd-sqlite ? – Prix Jul 15 '13 at 17:54
  • I don't know. I was hopping that dbd-sql had implemented something like attach but I have not seen anything in the docs and I wanted to be sure I was not missing something obvious. – Pablo Marin-Garcia Jul 15 '13 at 18:03
  • 1
    I see, well you can also look at the source at CPAN as well and I have not seen that possibility so you would have to create multiple instances and compare with each other. – Prix Jul 15 '13 at 18:04
  • In case there is no solution with dbd-sqlite I can always use http://stackoverflow.com/questions/11733581/merge-all-sqlite-databases-with-different-tables recipe and dump all dbs into one. The tables have different names. – Pablo Marin-Garcia Jul 15 '13 at 18:10

2 Answers2

5

do executes arbitrary SQL statements.

$dbh->do('attach foobar as foobar');

foobar's tables are then queryable.

daxim
  • 39,270
  • 4
  • 65
  • 132
2

You can even do this:

use DBI;
my $dbfile1 = 'test1.db'; # will be `main`
my $dbfile2 = 'test2.db'; # will attach as `other`
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile1","","") or die "dbh";
$dbh->do('attach ? as ?', undef, $dbfile2, 'other') or die "attach";