Skip to content

Latest commit

 

History

History
245 lines (166 loc) · 5.1 KB

57.md

File metadata and controls

245 lines (166 loc) · 5.1 KB

使用 Perl 获取 SQLite 元数据

原文: http://zetcode.com/db/sqliteperltutorial/meta/

元数据是有关数据库中数据的信息。 SQLite 中的元数据包含有关表和列的信息,我们在其中存储数据。 受 SQL 语句影响的行数是元数据。 结果集中返回的行数和列数也属于元数据。

可以使用PRAGMA命令获取 SQLite 中的元数据。 SQLite 对象可能具有属性,即元数据。 最后,我们还可以通过查询 SQLite 系统sqlite_master表来获取特定的元数据。

方法名称 描述
column_info() 提供有关列的信息
table_info() 提供有关表的信息
primary_key_info() 提供有关表中主键的信息
foreign_key_info() 提供有关表中外键的信息

上表列出了四种用于检索元数据的 Perl DBI 方法。

#!/usr/bin/perl

use strict;
use DBI;

my $dbh = DBI->connect(          
    "dbi:SQLite:dbname=test.db",          
    "",
    "",     
    { RaiseError => 1 } 
) or die $DBI::errstr;

my $sth = $dbh->primary_key_info(undef, "main", "Cars");
my @ary = $sth->fetchrow_array();

print join(" ", @ary), "\n";

$sth->finish();
$dbh->disconnect();

在第一个示例中,我们将在Cars表中找到有关主键的信息。

my $sth = $dbh->primary_key_info(undef, "main", "Cars");

primary_key_info()返回一个活动语句句柄,该句柄可用于获取有关构成表主键的列的信息。

my @ary = $sth->fetchrow_array();

从语句句柄,我们检索信息。

$ ./pkinfo.pl
 main Cars Id 1 PRIMARY KEY

从输出中我们可以看到Cars表中有一个主键。 主键是第一列,名为Id

在下一个示例中,我们将找到有关Cars表的一些数据。

#!/usr/bin/perl

use strict;
use DBI;

my $dbh = DBI->connect(          
    "dbi:SQLite:dbname=test.db",  
    "",
    "",                  
    { RaiseError => 1 }
) or die $DBI::errstr;

my $sth = $dbh->prepare( "PRAGMA table_info(Cars)" );  
$sth->execute();

my @row;
while (@row = $sth->fetchrow_array()) {
    print "@row\n";
}

$sth->finish();
$dbh->disconnect();

在此示例中,我们发出PRAGMA table_info(tableName)命令,以获取有关Cars表的一些元数据信息。

my $sth = $dbh->prepare( "PRAGMA table_info(Cars)" );  
$sth->execute();

PRAGMA table_info(Cars)命令为Cars表中的每一列返回一行。 结果集中的列包括列顺序号,列名称,数据类型,该列是否可以为NULL以及该列的默认值。

my @row;
while (@row = $sth->fetchrow_array()) {
    print "@row\n";
}

我们打印选定的数据。

$ ./pragma_table.pl 
0 Id INT 0  0
1 Name TEXT 0  0
2 Price INT 0  0

示例的输出。

接下来,我们将打印Cars表中的所有行及其列名。

#!/usr/bin/perl

use strict;
use DBI;

my $dbh = DBI->connect(          
    "dbi:SQLite:dbname=test.db", 
    "",                          
    "",                          
    { RaiseError => 1 },         
) or die $DBI::errstr;

my $sth = $dbh->prepare( "SELECT * FROM Cars LIMIT 8" );  
my $headers = $sth->{NAME};

my ($id, $name, $price) = @$headers;
printf  "%s %-10s %s\n", $id, $name, $price;

$sth->execute();

my $row;
while($row = $sth->fetchrow_hashref()) {
    printf "%2d %-10s %d\n", $row->{Id}, $row->{Name}, $row->{Price};
}

$sth->finish();
$dbh->disconnect();

我们将Cars表的内容打印到控制台。 现在,我们也包括列的名称。 记录与列名对齐。

my $headers = $sth->{NAME};

我们从语句对象获得列名。

my ($id, $name, $price) = @$headers;
printf "%s %-10s %s\n", $id, $name, $price;

列名将打印到控制台。 我们使用printf函数应用一些格式。

my $row;
while($row = $sth->fetchrow_hashref()) {
    printf "%2d %-10s %d\n", $row->{Id}, $row->{Name}, $row->{Price};
}

数据被检索,格式化并打印到终端。

$ ./columnheaders.pl
Id Name       Price
 1 Audi       52642
 2 Mercedes   57127
 3 Skoda      9000
 4 Volvo      29000
 5 Bentley    350000
 6 Citroen    21000
 7 Hummer     41400
 8 Volkswagen 21600

输出。

在与元数据有关的最后一个示例中,我们将列出test.db数据库中的所有表。

#!/usr/bin/perl

use strict;
use DBI;

my $dbh = DBI->connect(          
    "dbi:SQLite:dbname=test.db", 
    "",                          
    "",                          
    { RaiseError => 1 },         
) or die $DBI::errstr;

my @tables = $dbh->tables(); 

foreach my $table ( @tables ) {
     print "Table: $table\n"; 
}

$dbh->disconnect();

该代码示例将当前数据库中的所有可用表打印到终端。

my @tables = $dbh->tables();

表名使用tables()方法检索。

$ ./list_tables.pl
Table: "main"."sqlite_master"
Table: "temp"."sqlite_temp_master"
Table: "main"."Cars"
Table: "main"."Friends"
Table: "main"."Images"

这些是我们系统上的表。

在 SQLite Perl 教程的这一部分中,我们使用了数据库元数据。