在Perl中訪問SQLite數據庫
SQLite是一個零配置、無服務端、基于文件的事務型數據庫系統。由于它的輕量級,自包含和緊湊的設計,所以當你想要集成數據庫到你的程序中時,SQLite是一個非常不錯的選擇。在這篇文章中,我會展示如何用Perl腳本來創建和訪問SQLite數據庫。我演示的Perl代碼片段是完整的,所以你可以很簡單地修改并集成到你的項目中。
訪問SQLite的準備
我會使用SQLite DBI Perl驅動來連接到SQLite3。因此你需要在Linux中安裝它(和SQLite3一起)。
Debian、 Ubuntu 或者 Linux Mint
$ sudo apt-get install sqlite3 libdbd-sqlite3-perl
CentOS、 Fedora 或者 RHEL
$ sudo yum install sqlite perl-DBD-SQLite
安裝后,你可以檢查SQLite驅動可以通過下面的腳本訪問到。
#!/usr/bin/perlmy @drv = DBI->available_drivers(); print join("\n", @drv), "\n";</pre>
如果你運行腳本,你應該會看見下面的輸出。
DBM ExampleP File Gofer Proxy SQLite SpongePerl SQLite 訪問示例
下面就是Perl訪問SQLite的示例。這個Perl腳本會演示下面這些SQLite數據庫的常規管理。
- 創建和連接SQLite數據庫
- 在SQLite數據庫中創建新表
- 在表中插入行
- 在表中搜索和迭代行
- 在表中更新行
- 在表中刪除行 </ul>
use DBI; use strict;定義數據庫名稱和驅動
my $driver = "SQLite"; my $db_name = "xmodulo.db"; my $dbd = "DBI:$driver:dbname=$db_name";
sqlite 沒有用戶名密碼的概念
my $username = ""; my $password = "";
創建并連接到數據庫
以下創建的文件名為 xmodulo.db
my $dbh = DBI->connect($dbd, $username, $password, { RaiseError => 1 }) or die $DBI::errstr; print STDERR "Database opened successfully\n";
創建表
my $stmt = qq(CREATE TABLE IF NOT EXISTS NETWORK (ID INTEGER PRIMARY KEY AUTOINCREMENT, HOSTNAME TEXT NOT NULL, IPADDRESS INT NOT NULL, OS CHAR(50), CPULOAD REAL);); my $ret = $dbh->do($stmt); if($ret < 0) { print STDERR $DBI::errstr; } else { print STDERR "Table created successfully\n"; }
插入三行到表中
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('xmodulo', 16843009, 'Ubuntu 14.10', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('bert', 16843010, 'CentOS 7', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('puppy', 16843011, 'Ubuntu 14.10', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
在表中檢索行
$stmt = qq(SELECT id, hostname, os, cpuload from NETWORK;); my $obj = $dbh->prepare($stmt); $ret = $obj->execute() or die $DBI::errstr;
if($ret < 0) { print STDERR $DBI::errstr; } while(my @row = $obj->fetchrow_array()) { print "ID: ". $row[0] . "\n"; print "HOSTNAME: ". $row[1] ."\n"; print "OS: ". $row[2] ."\n"; print "CPULOAD: ". $row[3] ."\n\n"; }
更新表中的某行
$stmt = qq(UPDATE NETWORK set CPULOAD = 50 where OS='Ubuntu 14.10';); $ret = $dbh->do($stmt) or die $DBI::errstr;
if( $ret < 0 ) { print STDERR $DBI::errstr; } else { print STDERR "A total of $ret rows updated\n"; }
從表中刪除某行
$stmt = qq(DELETE from NETWORK where ID=2;); $ret = $dbh->do($stmt) or die $DBI::errstr;
if($ret < 0) { print STDERR $DBI::errstr; } else { print STDERR "A total of $ret rows deleted\n"; }
斷開數據庫連接
$dbh->disconnect(); print STDERR "Exit the database\n";</pre>
上面的Perl腳本運行成功后會創建一個叫“xmodulo.db”的數據庫文件,并會有下面的輸出。
Database opened successfully Table created successfully ID: 1 HOSTNAME: xmodulo OS: Ubuntu 14.10 CPULOAD: 0ID: 2 HOSTNAME: bert OS: CentOS 7 CPULOAD: 0
ID: 3 HOSTNAME: puppy OS: Ubuntu 14.10 CPULOAD: 0
A total of 2 rows updated A total of 1 rows deleted Exit the database</pre>
錯誤定位
如果你嘗試沒有安裝SQLite DBI驅動的情況下使用Perl訪問SQLite的話,你會遇到下面的錯誤。你必須按開始說的安裝DBI驅動。
Can't locate DBI.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./script.pl line 3. BEGIN failed--compilation aborted at ./script.pl line 3.
via: http://xmodulo.com/access-sqlite-database-perl.html
來源: https://linux.cn/article-5764-1.html