<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 Perl 獲取 SQLite 元數據 > 原文: [http://zetcode.com/db/sqliteperltutorial/meta/](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 方法。 ```perl #!/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`表中找到有關主鍵的信息。 ```perl my $sth = $dbh->primary_key_info(undef, "main", "Cars"); ``` `primary_key_info()`返回一個活動語句句柄,該句柄可用于獲取有關構成表主鍵的列的信息。 ```perl my @ary = $sth->fetchrow_array(); ``` 從語句句柄,我們檢索信息。 ```perl $ ./pkinfo.pl main Cars Id 1 PRIMARY KEY ``` 從輸出中我們可以看到`Cars`表中有一個主鍵。 主鍵是第一列,名為`Id`。 在下一個示例中,我們將找到有關`Cars`表的一些數據。 ```perl #!/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`表的一些元數據信息。 ```perl my $sth = $dbh->prepare( "PRAGMA table_info(Cars)" ); $sth->execute(); ``` `PRAGMA table_info(Cars)`命令為`Cars`表中的每一列返回一行。 結果集中的列包括列順序號,列名稱,數據類型,該列是否可以為`NULL`以及該列的默認值。 ```perl my @row; while (@row = $sth->fetchrow_array()) { print "@row\n"; } ``` 我們打印選定的數據。 ```perl $ ./pragma_table.pl 0 Id INT 0 0 1 Name TEXT 0 0 2 Price INT 0 0 ``` 示例的輸出。 接下來,我們將打印`Cars`表中的所有行及其列名。 ```perl #!/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`表的內容打印到控制臺。 現在,我們也包括列的名稱。 記錄與列名對齊。 ```perl my $headers = $sth->{NAME}; ``` 我們從語句對象獲得列名。 ```perl my ($id, $name, $price) = @$headers; printf "%s %-10s %s\n", $id, $name, $price; ``` 列名將打印到控制臺。 我們使用`printf`函數應用一些格式。 ```perl my $row; while($row = $sth->fetchrow_hashref()) { printf "%2d %-10s %d\n", $row->{Id}, $row->{Name}, $row->{Price}; } ``` 數據被檢索,格式化并打印到終端。 ```perl $ ./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`數據庫中的所有表。 ```perl #!/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(); ``` 該代碼示例將當前數據庫中的所有可用表打印到終端。 ```perl my @tables = $dbh->tables(); ``` 表名使用`tables()`方法檢索。 ```perl $ ./list_tables.pl Table: "main"."sqlite_master" Table: "temp"."sqlite_temp_master" Table: "main"."Cars" Table: "main"."Friends" Table: "main"."Images" ``` 這些是我們系統上的表。 在 SQLite Perl 教程的這一部分中,我們使用了數據庫元數據。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看