SQLite-Perl
安裝
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
DBI 介面API
以下是重要的DBI 程序,可滿足您在Perl 程式中使用SQLite 資料庫的需求。如果您需要了解更多細節,請查看 Perl DBI 官方文件。
序號 | #API & 描述 |
---|---|
#1 | DBI-> ;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() 開啟的資料庫連線。 |
連接資料庫
下面的 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
建立表格
下面的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。最後使用 make 和 make 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 Ding ##make and do make install to resolve the problem.
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 successfullySELECT 操作#下面的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 successfullyUPDATE 動作# 下方的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 successfullyDELETE 操作下面的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