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.

4
  • Why do you think you can load multiple databases at once with one instance of dbd-sqlite ? Commented Jul 15, 2013 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. Commented Jul 15, 2013 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. Commented Jul 15, 2013 at 18:04
  • In case there is no solution with dbd-sqlite I can always use stackoverflow.com/questions/11733581/… recipe and dump all dbs into one. The tables have different names. Commented Jul 15, 2013 at 18:10

2 Answers 2

5

do executes arbitrary SQL statements.

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

foobar's tables are then queryable.

Sign up to request clarification or add additional context in comments.

Comments

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.