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("
",@drv),"
";
如果你运行脚本,你应该会看见下面的输出。
DBMExamplePFileGoferProxySQLiteSponge
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.dbmy $dbh = DBI->connect($dbd, $username, $password,{RaiseError=>1})ordie $DBI::errstr;print STDERR "Database opened successfully
";# 创建表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
";}# 插入三行到表中$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)VALUES ("xmodulo",16843009,"Ubuntu 14.10",0.0));$ret = $dbh->do($stmt)ordie $DBI::errstr;$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)VALUES ("bert",16843010,"CentOS 7",0.0));$ret = $dbh->do($stmt)ordie $DBI::errstr;$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)VALUES ("puppy",16843011,"Ubuntu 14.10",0.0));$ret = $dbh->do($stmt)ordie $DBI::errstr;# 在表中检索行$stmt = qq(SELECT id, hostname, os, cpuload from NETWORK;);my $obj = $dbh->prepare($stmt);$ret = $obj->execute()ordie $DBI::errstr;if($ret <0){print STDERR $DBI::errstr;}while(my@row= $obj->fetchrow_array()){print"ID: ". $row[0]."
";print"HOSTNAME: ". $row[1]."
";print"OS: ". $row[2]."
";print"CPULOAD: ". $row[3]."
";}# 更新表中的某行$stmt = qq(UPDATE NETWORK set CPULOAD =50where OS="Ubuntu 14.10";);$ret = $dbh->do($stmt)ordie $DBI::errstr;if( $ret <0){print STDERR $DBI::errstr;}else{print STDERR "A total of $ret rows updated
";}# 从表中删除某行$stmt = qq(DELETE from NETWORK where ID=2;);$ret = $dbh->do($stmt)ordie $DBI::errstr;if($ret <0){print STDERR $DBI::errstr;}else{print STDERR "A total of $ret rows deleted
";}# 断开数据库连接$dbh->disconnect();print STDERR "Exit the database
";
上面的Perl脚本运行成功后会创建一个叫“xmodulo.db”的数据库文件,并会有下面的输出。
Database opened successfullyTable created successfullyID:1HOSTNAME: xmoduloOS:Ubuntu14.10CPULOAD:0ID:2HOSTNAME: bertOS:CentOS7CPULOAD:0ID:3HOSTNAME: puppyOS:Ubuntu14.10CPULOAD:0A total of 2 rows updatedA total of 1 rows deletedExit 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.
SQLite3 安装、基本操作 http://www.linuxidc.com/Linux/2012-05/60452.htmUbuntu 12.04下SQLite数据库简单应用 http://www.linuxidc.com/Linux/2012-06/63379.htmUbuntu 12.04下安装 SQLite及其使用方法 http://www.linuxidc.com/Linux/2013-08/89155.htmSQLite 数据库入门基础教程 http://www.linuxidc.com/Linux/2014-06/103587.htm
SQLite 的详细介绍:请点这里
SQLite 的下载地址:请点这里
via: http://xmodulo.com/access-sqlite-database-perl.html作者:Dan Nanni 译者:geekpi 校对:wxy本文由 LCTT 原创翻译,Linux中国 荣誉推出
本文永久更新链接地址