SQLite Perl接口
在本章中,您将学习如何在Perl程序中使用SQLite。
安装
可以使用Perl DBI模块将SQLite3集成到Perl中,该模块是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
数据库接口API
以下是一些重要的DBI例程,可以满足您在Perl程序中使用SQLite数据库的要求。如果您正在寻找更复杂的应用程序,您可以查看Perl DBI官方文档。
API | 描述 |
---|---|
DBI- >connect($data_source, "", "", \%attr) |
在请求的 $data_source 上建立数据库连接或会话。如果连接成功,则返回一个数据库句柄对象。 数据源的格式如下− DBI:SQLite:dbname = ‘test.db’ 其中SQLite是SQLite驱动程序的名称,test.db是SQLite数据库文件的名称。如果文件名为 ‘:memory:’ ,则会创建一个在RAM中持续时间仅为会话期间的内存数据库。 如果文件名是实际设备文件名,则尝试使用其值打开数据库文件。如果不存在该名称的文件,则会创建一个新的数据库文件。 将第二个和第三个参数保留为空字符串,最后一个参数是通过以下示例中的各种属性。 |
$dbh- >do($sql) |
此例程准备并执行一个SQL语句。返回受影响的行数或错误。返回值为-1表示行数未知,不适用或不可用。这里,$dbh 是由DBI->connect()调用返回的句柄。 |
$dbh- >prepare($sql) |
此例程为以后由数据库引擎执行准备一个语句,并返回对语句处理对象的引用。 |
$sth- >execute() |
此例程执行必要的处理以执行准备好的语句。如果出错,则返回undef。无论受影响的行数如何,成功的执行总是返回true。这里,$sth 是由$dbh->prepare($sql )调用返回的语句处理对象。 |
$sth->fetchrow_array() |
这个函数获取下一行数据,并将其作为包含字段值的列表返回。空字段在列表中以undef值返回。 |
$DBI::err |
这与$h->err 等效,其中$h可以是任何句柄类型,如$dbh ,$sth 或$drh 。它返回最后调用的驱动程序方法的本机数据库引擎错误代码。 |
$DBI::errstr |
这与$h->errstr 等效,其中$h可以是任何句柄类型,如$dbh ,$sth 或$drh 。它返回最后调用的DBI方法的本机数据库引擎错误消息。 |
$dbh->disconnect() |
这个例程关闭一个之前通过调用DBI->connect()打开的数据库连接。 |
连接数据库
以下Perl代码演示了如何连接到现有数据库。如果数据库不存在,则会创建一个新数据库,并最终返回一个数据库对象。
#!/usr/bin/perl
use DBI;
use strict;
my driver = "SQLite";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 }) or dieDBI::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";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 })
or dieDBI::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););
myrv = dbh->do(stmt);
if(rv<0) {
printDBI::errstr;
} else {
print "Table created successfully\n";
}
$dbh->disconnect();
当上述程序执行时,它将在你的test.db数据库中创建一个名为COMPANY的表,并且将显示以下信息:
Opened database successfully
Table created successfully
注意 − 如果您在任何操作中看到以下错误 −
DBD::SQLite::st execute failed: not an error(21) at dbdimp.c line 398
在这种情况下,打开 dbdimp.c文件 ,该文件位于DBD-SQLite安装中,查找 sqlite3_prepare() 函数并将其第三个参数更改为 -1 而不是0。最后,使用 make 安装DBD::SQLite并执行 make install 来解决问题。
插入操作
以下Perl程序演示了如何在上述示例中创建COMPANY表的记录。
#!/usr/bin/perl
use DBI;
use strict;
my driver = "SQLite";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 })
or dieDBI::errstr;
print "Opened database successfully\n";
my stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 ));
myrv = 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 dieDBI::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 dieDBI::errstr;
print "Records created successfully\n";
$dbh->disconnect();
当上述程序被执行时,它将在公司表中创建给定的记录,并显示以下两行-
Opened database successfully
Records created successfully
SELECT操作
以下Perl程序显示了如何从上面示例中创建的COMPANY表中提取和显示记录。
#!/usr/bin/perl
use DBI;
use strict;
my driver = "SQLite";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 })
or dieDBI::errstr;
print "Opened database successfully\n";
my stmt = qq(SELECT id, name, address, salary from COMPANY;);
mysth = 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 successfully
更新操作
以下Perl代码展示了如何使用UPDATE语句来更新任意记录,并从COMPANY表中获取和显示更新后的记录。
#!/usr/bin/perl
use DBI;
use strict;
my driver = "SQLite";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 })
or dieDBI::errstr;
print "Opened database successfully\n";
my stmt = qq(UPDATE COMPANY set SALARY = 25000.00 where ID=1;);
myrv = 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;);
mysth = 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 successfully
删除操作
以下Perl代码展示了如何使用DELETE语句删除数据表COMPANY中的记录,并获取和显示剩余的记录。
#!/usr/bin/perl
use DBI;
use strict;
my driver = "SQLite";
mydatabase = "test.db";
my dsn = "DBI:driver:dbname=database";
myuserid = "";
my password = "";
mydbh = DBI->connect(dsn,userid, password, { RaiseError => 1 })
or dieDBI::errstr;
print "Opened database successfully\n";
my stmt = qq(DELETE from COMPANY where ID = 2;);
myrv = 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;);
mysth = 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