【问题标题】:how to attach another sqlite db to $dbh with Perl dbd-sqlite如何使用 Perl dbd-sqlite 将另一个 sqlite db 附加到 $dbh
【发布时间】:2015-12-17 00:04:57
【问题描述】:

如何在 Perl 中将多个 sqlite 数据库附加到单个 $dbh 中?在命令行中我可以在交互式 sqlite3 rpel 中进行附加,在 Perl 中使用 dbd-sqlite 怎么样?

对不起,如果这里已经回答过,perlmonks 或类似的,但无法找到正确的答案。

【问题讨论】:

  • 为什么你认为你可以用一个 dbd-sqlite 实例一次加载多个数据库?
  • 我不知道。我希望 dbd-sql 已经实现了类似 attach 的东西,但我在文档中没有看到任何东西,我想确保我没有遗漏一些明显的东西。
  • 我明白了,您也可以查看 CPAN 的源代码,但我没有看到这种可能性,因此您必须创建多个实例并相互比较。
  • 如果 dbd-sqlite 没有解决方案,我总是可以使用 stackoverflow.com/questions/11733581/… recipe 并将所有 dbs 转储到一个中。这些表有不同的名称。

标签: perl sqlite


【解决方案1】:

do 执行任意 SQL 语句。

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

foobar 的表随后可查询。

【讨论】:

    【解决方案2】:

    你甚至可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多