如何从 Perl 的 DBI 获取模式?

发布于 2024-09-03 12:36:16 字数 171 浏览 4 评论 0原文

我正在使用 Perl DBI。我知道 $dbase->tables() 将返回相应数据库中的所有表。同样,我想知道数据库中可用的模式。有没有可用的功能?

I am using Perl DBI. I know that $dbase->tables() will return all the tables in the corresponding database. Likewise, I want to know the schemas available in the database. Is there any function available for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人的歌 2024-09-10 12:36:16

您要查找的是:DBI->table_info()

像这样调用它:

my $sth = $dbh->table_info('', '%', '');
my $schemas = $dbh->selectcol_arrayref($sth, {Columns => [2]});
print "Schemas: ", join ', ', @$schemas;

What you're looking for is: DBI->table_info()

Call it like this:

my $sth = $dbh->table_info('', '%', '');
my $schemas = $dbh->selectcol_arrayref($sth, {Columns => [2]});
print "Schemas: ", join ', ', @$schemas;
却一份温柔 2024-09-10 12:36:16

这有效。

创建数据库:

echo 'create table foo (bar integer primary key, quux varchar(30));' | sqlite3 foobar.sqlite

Perl 程序打印模式:

use 5.010;
use Data::Dumper qw(Dumper);
use DBIx::Class::Schema::Loader qw();
DBIx::Class::Schema::Loader->naming('current');
DBIx::Class::Schema::Loader->use_namespaces(1);

my $dbi_dsn = 'dbi:SQLite:dbname=foobar.sqlite';
my ($dbi_user, $dbi_pass);
my $schema = DBIx::Class::Schema::Loader->connect(
    $dbi_dsn, $dbi_user, $dbi_pass, {'AutoCommit' => 1, 'RaiseError' => 1,}
);

for my $source_name ($schema->sources) {
    say "*** Source: $source_name";
    my $result_source = $schema->source($source_name);
    for my $column_name ($result_source->columns) {
        say "Column: $column_name";
        say Dumper $result_source->column_info($column_name);
    }
}

输出:

*** Source: Foo
Column: bar
$VAR1 = {
          'data_type' => 'integer',
          'is_auto_increment' => 1,
          'is_nullable' => 1
        };

Column: quux
$VAR1 = {
          'data_type' => 'varchar',
          'is_nullable' => 1,
          'size' => 30
        };

This works.

Create a database:

echo 'create table foo (bar integer primary key, quux varchar(30));' | sqlite3 foobar.sqlite

Perl program to print schema:

use 5.010;
use Data::Dumper qw(Dumper);
use DBIx::Class::Schema::Loader qw();
DBIx::Class::Schema::Loader->naming('current');
DBIx::Class::Schema::Loader->use_namespaces(1);

my $dbi_dsn = 'dbi:SQLite:dbname=foobar.sqlite';
my ($dbi_user, $dbi_pass);
my $schema = DBIx::Class::Schema::Loader->connect(
    $dbi_dsn, $dbi_user, $dbi_pass, {'AutoCommit' => 1, 'RaiseError' => 1,}
);

for my $source_name ($schema->sources) {
    say "*** Source: $source_name";
    my $result_source = $schema->source($source_name);
    for my $column_name ($result_source->columns) {
        say "Column: $column_name";
        say Dumper $result_source->column_info($column_name);
    }
}

Output:

*** Source: Foo
Column: bar
$VAR1 = {
          'data_type' => 'integer',
          'is_auto_increment' => 1,
          'is_nullable' => 1
        };

Column: quux
$VAR1 = {
          'data_type' => 'varchar',
          'is_nullable' => 1,
          'size' => 30
        };
野稚 2024-09-10 12:36:16

使用 ODBC 连接 Oracle 数据库,我必须对 Arnie 叔叔的答案使用这种变体:

my $table_info = $dbh->table_info(undef, '%', undef);
my $schemas    = $table_info->fetchall_arrayref([1]);

print "Schemas :\n",
      join( "\n", map {$_->[0]} @$schemas ), "\n";

否则,在尝试使用 selectcol_arrayref($sth, ...) 时,$schemas 将是未定义的代码>.

Using ODBC to an Oracle database, I had to use this variation on Uncle Arnie's answer:

my $table_info = $dbh->table_info(undef, '%', undef);
my $schemas    = $table_info->fetchall_arrayref([1]);

print "Schemas :\n",
      join( "\n", map {$_->[0]} @$schemas ), "\n";

Otherwise, $schemas would be undefined when trying to use selectcol_arrayref($sth, ...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文