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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用 Perl 獲取 MySQL 元數據 > 原文: [http://zetcode.com/db/mysqlperl/meta/](http://zetcode.com/db/mysqlperl/meta/) 元數據是有關數據庫中數據的信息。 MySQL 中的元數據包含有關存儲數據的表和列的信息。 受 SQL 語句影響的行數是元數據。 結果集中返回的行數和列數也屬于元數據。 | 方法名稱 | 描述 | | --- | --- | | `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:mysql:dbname=mydb", "user12", "34klq*", { RaiseError => 1 }, ) or die $DBI::errstr; my $sth = $dbh->primary_key_info(undef, "mydb", "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 $ ./pk_info.pl mydb Cars Id 1 PRIMARY ``` 從輸出中我們可以看到`Cars`表中有一個主鍵。 主鍵是第一列,名為`Id`。 接下來,我們將打印`Cars`表中的所有行及其列名。 ```perl #!/usr/bin/perl use strict; use DBI; my $dbh = DBI->connect( "dbi:mysql:dbname=mydb", "user12", "34klq*", { RaiseError => 1 }, ) or die $DBI::errstr; my $sth = $dbh->prepare( "SELECT * FROM Cars LIMIT 8" ); $sth->execute(); my $headers = $sth->{NAME}; my ($id, $name, $price) = @$headers; printf "%s %-10s %s\n", $id, $name, $price; 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 $ ./column_names.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 21601 ``` `column_names.pl`腳本的輸出。 在與元數據有關的最后一個示例中,我們將列出`test.db`數據庫中的所有表。 ```perl #!/usr/bin/perl use strict; use DBI; my $dbh = DBI->connect( "dbi:mysql:dbname=mydb", "user12", "34klq*", { 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: `mydb`.`Cars` Table: `mydb`.`Friends` Table: `mydb`.`Images` ``` 這些是我們系統上的表。 在 MySQL 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>

                              哎呀哎呀视频在线观看