Linux中國
如何用Perl訪問SQLite資料庫
訪問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/perl
my @drv = DBI->available_drivers();
print join("n", @drv), "n";
如果你運行腳本,你應該會看見下面的輸出。
DBM
ExampleP
File
Gofer
Proxy
SQLite
Sponge
Perl SQLite 訪問示例
下面就是Perl訪問SQLite的示例。這個Perl腳本會演示下面這些SQLite資料庫的常規管理。
- 創建和連接SQLite資料庫
- 在SQLite資料庫中創建新表
- 在表中插入行
- 在表中搜索和迭代行
- 在表中更新行
- 在表中刪除行
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 successfullyn";
# 創建表
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 successfullyn";
}
# 插入三行到表中
$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] ."nn";
}
# 更新表中的某行
$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 updatedn";
}
# 從表中刪除某行
$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 deletedn";
}
# 斷開資料庫連接
$dbh->disconnect();
print STDERR "Exit the databasen";
上面的Perl腳本運行成功後會創建一個叫「xmodulo.db」的資料庫文件,並會有下面的輸出。
Database opened successfully
Table created successfully
ID: 1
HOSTNAME: xmodulo
OS: Ubuntu 14.10
CPULOAD: 0
ID: 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
錯誤定位
如果你嘗試沒有安裝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
本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive
對這篇文章感覺如何?
太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
More in:Linux中國
捐贈 Let's Encrypt,共建安全的互聯網
隨著 Mozilla、蘋果和谷歌對沃通和 StartCom 這兩家 CA 公司處罰落定,很多使用這兩家 CA 所簽發證書的網站紛紛尋求新的證書籤發商。有一個非盈利組織可以為大家提供了免費、可靠和安全的 SSL 證書服務,這就是 Let's Encrypt 項目。現在,它需要您的幫助
Let's Encrypt 正式發布,已經保護 380 萬個域名
由於 Let's Encrypt 讓安裝 X.509 TLS 證書變得非常簡單,所以這個數量增長迅猛。
關於Linux防火牆iptables的面試問答
Nishita Agarwal是Tecmint的用戶,她將分享關於她剛剛經歷的一家公司(印度的一家私人公司Pune)的面試經驗。在面試中她被問及許多不同的問題,但她是iptables方面的專家,因此她想分享這些關於iptables的問題和相應的答案給那些以後可能會進行相關面試的人。 所有的問題和相應的答案都基於Nishita Agarwal的記憶並經過了重寫。 嗨,朋友!我叫Nishita Agarwal。我已經取得了理學學士學位,我的專業集中在UNIX和它的變種(BSD,Linux)。它們一直深深的吸引著我。我在存儲方面有1年多的經驗。我正在尋求職業上的變化,並將供職於印度的P
Lets Encrypt 已被所有主流瀏覽器所信任
旨在讓每個網站都能使用 HTTPS 加密的非贏利組織 Lets Encrypt 已經得了 IdenTrust的交叉簽名,這意味著其證書現在已經可以被所有主流的瀏覽器所信任。從這個裡程碑事件開始,訪問者訪問使用了Lets Encrypt 證書的網站不再需要特別配置就可以得到 HTTPS 安全保護了。 Lets Encrypt 的兩個中級證書 ...

















