精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

SQLite Perl 編程接口

sqlite perl 編程接口

 

1. 安裝

sqlite3 可使用 perl dbi 模塊與 perl 進行集成。perl dbi 模塊是 perl 編程語言的數據庫訪問模塊。它定義了一組提供標準數據庫接口的方法、變量及規則。

下面顯示了在 linux/unix 機器上安裝 dbi 模塊的簡單步驟:

$ wget http://search.cpan.org/cpan/authors/id/t/ti/timb/dbi-1.625.tar.gz
$ tar xvfz dbi-1.625.tar.gz
$ cd dbi-1.625
$ perl makefile.pl
$ make
$ make install

如果您需要為 dbi 安裝 sqlite 驅動程序,那么可按照以下步驟進行安裝:

$ wget http://search.cpan.org/cpan/authors/id/m/ms/msergeant/dbd-sqlite-1.11.tar.gz
$ tar xvfz dbd-sqlite-1.11.tar.gz
$ cd dbd-sqlite-1.11
$ perl makefile.pl
$ make
$ make install

 

2. dbi 接口 api

以下是重要的 dbi 程序,可以滿足您在 perl 程序中使用 sqlite 數據庫的需求。如果您需要了解更多細節,請查看 perl dbi 官方文檔。

序號api & 描述
1dbi->connect($data_source, "", "", \%attr)

建立一個到被請求的 $data_source 的數據庫連接或者 session。如果連接成功,則返回一個數據庫處理對象。

數據源形式如下所示:dbi:sqlite:dbname='test.db'。其中,sqlite 是 sqlite 驅動程序名稱,test.db 是 sqlite 數據庫文件的名稱。如果文件名 filename 賦值為 ':memory:',那么它將會在 ram 中創建一個內存數據庫,這只會在 session 的有效時間內持續。

如果文件名 filename 為實際的設備文件名稱,那么它將使用這個參數值嘗試打開數據庫文件。如果該名稱的文件不存在,那么將創建一個新的命名為該名稱的數據庫文件。

您可以保留第二個和第三個參數為空白字符串,最后一個參數用于傳遞各種屬性,詳見下面的范例講解。

2$dbh->do($sql)

該例程準備并執行一個簡單的 sql 語句。返回受影響的行數,如果發生錯誤則返回 undef。返回值 -1 意味著行數未知,或不適用 ,或不可用。在這里,$dbh 是由 dbi->connect() 調用返回的處理。

3$dbh->prepare($sql)

該例程為數據庫引擎后續執行準備一個語句,并返回一個語句處理對象。

4$sth->execute()

該例程執行任何執行預準備的語句需要的處理。如果發生錯誤則返回 undef。如果成功執行,則無論受影響的行數是多少,總是返回 true。在這里,$sth 是由 $dbh->prepare($sql) 調用返回的語句處理。

5$sth->fetchrow_array()

該例程獲取下一行數據,并以包含各字段值的列表形式返回。在該列表中,null 字段將作為 undef 值返回。

6$dbi::err

這相當于 $h->err。其中,$h 是任何的處理類型,比如 $dbh、$sth 或 $drh。該程序返回最后調用的驅動程序(driver)方法的數據庫引擎錯誤代碼。

7$dbi::errstr

這相當于 $h->errstr。其中,$h 是任何的處理類型,比如 $dbh、$sth 或 $drh。該程序返回最后調用的 dbi 方法的數據庫引擎錯誤消息。

8$dbh->disconnect()

該例程關閉之前調用 dbi->connect() 打開的數據庫連接。

 

3. 連接數據庫

下面的 perl 代碼顯示了如何連接到一個現有的數據庫。如果數據庫不存在,那么它就會被創建,最后將返回一個數據庫對象。

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite"; 
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 }) 
                      or die $dbi::errstr;

print "opened database successfully\n";

現在,讓我們來運行上面的程序,在當前目錄中創建我們的數據庫 test.db。您可以根據需要改變路徑。保存上面代碼到 sqlite.pl 文件中,并按如下所示執行。如果數據庫成功創建,那么會顯示下面所示的消息:

$ chmod +x sqlite.pl
$ ./sqlite.pl
open database successfully

 

4. 創建表

下面的 perl 代碼段將用于在先前創建的數據庫中創建一個表:

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite";
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 })
                      or die $dbi::errstr;
print "opened database successfully\n";

my $stmt = qq(create table company
      (id int primary key     not null,
       name           text    not null,
       age            int     not null,
       address        char(50),
       salary         real););
my $rv = $dbh->do($stmt);
if($rv < 0){
   print $dbi::errstr;
} else {
   print "table created successfully\n";
}
$dbh->disconnect();

上述程序執行時,它會在 test.db 中創建 company 表,并顯示下面所示的消息:

opened database successfully
table created successfully

注意:如果您在任何操作中遇到了下面的錯誤: in case you see following error in any of the operation:

dbd::sqlite::st execute failed: not an error(21) at dbdimp.c line 398

在這種情況下,您已經在 dbd-sqlite 安裝中打開了可用的 dbdimp.c 文件,找到 sqlite3_prepare() 函數,并把它的第三個參數 0 改為 -1。最后使用 makemake install 安裝 dbd::sqlite,即可解決問題。 in this case you will have open dbdimp.c file available in dbd-sqlite installation and find out sqlite3_prepare() function and change its third argument to -1 instead of 0. finally install dbd::sqlite using make and do make install to resolve the problem.

 

5. insert 操作

下面的 perl 程序顯示了如何在上面創建的 company 表中創建記錄:

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite";
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 })
                      or die $dbi::errstr;
print "opened database successfully\n";

my $stmt = qq(insert into company (id,name,age,address,salary)
      values (1, 'paul', 32, 'california', 20000.00 ));
my $rv = $dbh->do($stmt) or die $dbi::errstr;

$stmt = qq(insert into company (id,name,age,address,salary)
      values (2, 'allen', 25, 'texas', 15000.00 ));
$rv = $dbh->do($stmt) or die $dbi::errstr;

$stmt = qq(insert into company (id,name,age,address,salary)
      values (3, 'teddy', 23, 'norway', 20000.00 ));
$rv = $dbh->do($stmt) or die $dbi::errstr;

$stmt = qq(insert into company (id,name,age,address,salary)
      values (4, 'mark', 25, 'rich-mond ', 65000.00 ););
$rv = $dbh->do($stmt) or die $dbi::errstr;

print "records created successfully\n";
$dbh->disconnect();

上述程序執行時,它會在 company 表中創建給定記錄,并會顯示以下兩行:

opened database successfully
records created successfully

 

6. select 操作

下面的 perl 程序顯示了如何從前面創建的 company 表中獲取并顯示記錄:

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite";
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 })
                      or die $dbi::errstr;
print "opened database successfully\n";

my $stmt = qq(select id, name, address, salary  from company;);
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $dbi::errstr;
if($rv < 0){
   print $dbi::errstr;
}
while(my @row = $sth->fetchrow_array()) {
      print "id = ". $row[0] . "\n";
      print "name = ". $row[1] ."\n";
      print "address = ". $row[2] ."\n";
      print "salary =  ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$dbh->disconnect();

上述程序執行時,它會產生以下結果:

opened database successfully
id = 1
name = paul
address = california
salary =  20000

id = 2
name = allen
address = texas
salary =  15000

id = 3
name = teddy
address = norway
salary =  20000

id = 4
name = mark
address = rich-mond
salary =  65000

operation done successfully

 

7. update 操作

下面的 perl 代碼顯示了如何使用 update 語句來更新任何記錄,然后從 company 表中獲取并顯示更新的記錄:

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite";
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 })
                      or die $dbi::errstr;
print "opened database successfully\n";

my $stmt = qq(update company set salary = 25000.00 where id=1;);
my $rv = $dbh->do($stmt) or die $dbi::errstr;
if( $rv < 0 ){
   print $dbi::errstr;
}else{
   print "total number of rows updated : $rv\n";
}
$stmt = qq(select id, name, address, salary  from company;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $dbi::errstr;
if($rv < 0){
   print $dbi::errstr;
}
while(my @row = $sth->fetchrow_array()) {
      print "id = ". $row[0] . "\n";
      print "name = ". $row[1] ."\n";
      print "address = ". $row[2] ."\n";
      print "salary =  ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$dbh->disconnect();

上述程序執行時,它會產生以下結果:

opened database successfully
total number of rows updated : 1
id = 1
name = paul
address = california
salary =  25000

id = 2
name = allen
address = texas
salary =  15000

id = 3
name = teddy
address = norway
salary =  20000

id = 4
name = mark
address = rich-mond
salary =  65000

operation done successfully

 

8. delete 操作

下面的 perl 代碼顯示了如何使用 delete 語句刪除任何記錄,然后從 company 表中獲取并顯示剩余的記錄:

#!/usr/bin/perl

use dbi;
use strict;

my $driver   = "sqlite";
my $database = "test.db";
my $dsn = "dbi:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = dbi->connect($dsn, $userid, $password, { raiseerror => 1 })
                      or die $dbi::errstr;
print "opened database successfully\n";

my $stmt = qq(delete from company where id=2;);
my $rv = $dbh->do($stmt) or die $dbi::errstr;
if( $rv < 0 ){
   print $dbi::errstr;
}else{
   print "total number of rows deleted : $rv\n";
}
$stmt = qq(select id, name, address, salary  from company;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $dbi::errstr;
if($rv < 0){
   print $dbi::errstr;
}
while(my @row = $sth->fetchrow_array()) {
      print "id = ". $row[0] . "\n";
      print "name = ". $row[1] ."\n";
      print "address = ". $row[2] ."\n";
      print "salary =  ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$dbh->disconnect();

上述程序執行時,它會產生以下結果:

opened database successfully
total number of rows deleted : 1
id = 1
name = paul
address = california
salary =  25000

id = 3
name = teddy
address = norway
salary =  20000

id = 4
name = mark
address = rich-mond
salary =  65000

operation done successfully

下一節:sqlite python 編程接口

sqlite教程

相關文章