SQLite3 可使用 Perl DBI 模塊與 Perl 進(jìn)行集成。Perl DBI 模塊是 Perl 編程語言的數(shù)據(jù)庫訪問模塊。它定義了一組提供標(biāo)準(zhǔn)數(shù)據(jù)庫接口的方法、變量及規(guī)則。
下面顯示了在 Linux/UNIX 機(jī)器上安裝 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 驅(qū)動程序,那么可按照以下步驟進(jìn)行安裝:
$ 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 程序,可以滿足您在 Perl 程序中使用 SQLite 數(shù)據(jù)庫的需求。如果您需要了解更多細(xì)節(jié),請查看 Perl DBI 官方文檔。
序號 | API & 描述 |
---|---|
1 | DBI->connect($data_source, "", "", \%attr) 建立一個(gè)到被請求的 $data_source 的數(shù)據(jù)庫連接或者 session。如果連接成功,則返回一個(gè)數(shù)據(jù)庫處理對象。 數(shù)據(jù)源形式如下所示:DBI:SQLite:dbname='test.db'。其中,SQLite 是 SQLite 驅(qū)動程序名稱,test.db 是 SQLite 數(shù)據(jù)庫文件的名稱。如果文件名 filename 賦值為 ':memory:',那么它將會在 RAM 中創(chuàng)建一個(gè)內(nèi)存數(shù)據(jù)庫,這只會在 session 的有效時(shí)間內(nèi)持續(xù)。 如果文件名 filename 為實(shí)際的設(shè)備文件名稱,那么它將使用這個(gè)參數(shù)值嘗試打開數(shù)據(jù)庫文件。如果該名稱的文件不存在,那么將創(chuàng)建一個(gè)新的命名為該名稱的數(shù)據(jù)庫文件。 您可以保留第二個(gè)和第三個(gè)參數(shù)為空白字符串,最后一個(gè)參數(shù)用于傳遞各種屬性,詳見下面的實(shí)例講解。 |
2 | $dbh->do($sql) 該例程準(zhǔn)備并執(zhí)行一個(gè)簡單的 SQL 語句。返回受影響的行數(shù),如果發(fā)生錯(cuò)誤則返回 undef。返回值 -1 意味著行數(shù)未知,或不適用 ,或不可用。在這里,$dbh 是由 DBI->connect() 調(diào)用返回的處理。 |
3 | $dbh->prepare($sql) 該例程為數(shù)據(jù)庫引擎后續(xù)執(zhí)行準(zhǔn)備一個(gè)語句,并返回一個(gè)語句處理對象。 |
4 | $sth->execute() 該例程執(zhí)行任何執(zhí)行預(yù)準(zhǔn)備的語句需要的處理。如果發(fā)生錯(cuò)誤則返回 undef。如果成功執(zhí)行,則無論受影響的行數(shù)是多少,總是返回 true。在這里,$sth 是由 $dbh->prepare($sql) 調(diào)用返回的語句處理。 |
5 | $sth->fetchrow_array() 該例程獲取下一行數(shù)據(jù),并以包含各字段值的列表形式返回。在該列表中,Null 字段將作為 undef 值返回。 |
6 | $DBI::err 這相當(dāng)于 $h->err。其中,$h 是任何的處理類型,比如 $dbh、$sth 或 $drh。該程序返回最后調(diào)用的驅(qū)動程序(driver)方法的數(shù)據(jù)庫引擎錯(cuò)誤代碼。 |
7 | $DBI::errstr 這相當(dāng)于 $h->errstr。其中,$h 是任何的處理類型,比如 $dbh、$sth 或 $drh。該程序返回最后調(diào)用的 DBI 方法的數(shù)據(jù)庫引擎錯(cuò)誤消息。 |
8 | $dbh->disconnect() 該例程關(guān)閉之前調(diào)用 DBI->connect() 打開的數(shù)據(jù)庫連接。 |
下面的 Perl 代碼顯示了如何連接到一個(gè)現(xiàn)有的數(shù)據(jù)庫。如果數(shù)據(jù)庫不存在,那么它就會被創(chuàng)建,最后將返回一個(gè)數(shù)據(jù)庫對象。
#!/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";
現(xiàn)在,讓我們來運(yùn)行上面的程序,在當(dāng)前目錄中創(chuàng)建我們的數(shù)據(jù)庫 test.db。您可以根據(jù)需要改變路徑。保存上面代碼到 sqlite.pl 文件中,并按如下所示執(zhí)行。如果數(shù)據(jù)庫成功創(chuàng)建,那么會顯示下面所示的消息:
$ chmod +x sqlite.pl $ ./sqlite.pl Open database successfully
下面的 Perl 代碼段將用于在先前創(chuàng)建的數(shù)據(jù)庫中創(chuàng)建一個(gè)表:
#!/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();
上述程序執(zhí)行時(shí),它會在 test.db 中創(chuàng)建 COMPANY 表,并顯示下面所示的消息:
Opened database successfully Table created successfully
注意:如果您在任何操作中遇到了下面的錯(cuò)誤: 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
在這種情況下,您已經(jīng)在 DBD-SQLite 安裝中打開了可用的 dbdimp.c 文件,找到 sqlite3_prepare() 函數(shù),并把它的第三個(gè)參數(shù) 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 DBD::SQLite using make and do make install to resolve the problem.
下面的 Perl 程序顯示了如何在上面創(chuàng)建的 COMPANY 表中創(chuàng)建記錄:
#!/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();
上述程序執(zhí)行時(shí),它會在 COMPANY 表中創(chuàng)建給定記錄,并會顯示以下兩行:
Opened database successfully Records created successfully
下面的 Perl 程序顯示了如何從前面創(chuàng)建的 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();
上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
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
下面的 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();
上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
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
下面的 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();
上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
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
更多建議: