sqlite perl 编程接口
1. 安装
sqlite3 可使用 perl dbi 模块与 perl 进行集成。perl dbi 模块是 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
2. dbi 接口 api
以下是重要的 dbi 程序,可以满足您在 perl 程序中使用 sqlite 数据库的需求。如果您需要了解更多细节,请查看 perl dbi 官方文档。
序号 | api & 描述 |
---|---|
1 | dbi->connect($data_source, "", "", \%attr)
建立一个到被请求的 $data_source 的数据库连接或者 session。如果连接成功,则返回一个数据库处理对象。 数据源形式如下所示:dbi:sqlite:dbname='test.db'。其中,sqlite 是 sqlite 驱动程序名称,test.db 是 sqlite 数据库文件的名称。如果文件名 filename 赋值为 ':memory:',那么它将会在 ram 中创建一个内存数据库,这只会在 session 的有效时间内持续。 如果文件名 filename 为实际的设备文件名称,那么它将使用这个参数值尝试打开数据库文件。如果该名称的文件不存在,那么将创建一个新的命名为该名称的数据库文件。 您可以保留第二个和第三个参数为空白字符串,最后一个参数用于传递各种属性,详见下面的范例讲解。 |
2 | $dbh->do($sql)
该例程准备并执行一个简单的 sql 语句。返回受影响的行数,如果发生错误则返回 undef。返回值 -1 意味着行数未知,或不适用 ,或不可用。在这里,$dbh 是由 dbi->connect() 调用返回的处理。 |
3 | $dbh->prepare($sql)
该例程为数据库引擎后续执行准备一个语句,并返回一个语句处理对象。 |
4 | $sth->execute()
该例程执行任何执行预准备的语句需要的处理。如果发生错误则返回 undef。如果成功执行,则无论受影响的行数是多少,总是返回 true。在这里,$sth 是由 $dbh->prepare($sql) 调用返回的语句处理。 |
5 | $sth->fetchrow_array()
该例程获取下一行数据,并以包含各字段值的列表形式返回。在该列表中,null 字段将作为 undef 值返回。 |
6 | $dbi::err
这相当于 $h->err。其中,$h 是任何的处理类型,比如 $dbh、$sth 或 $drh。该程序返回最后调用的驱动程序(driver)方法的数据库引擎错误代码。 |
7 | $dbi::errstr
这相当于 $h->errstr。其中,$h 是任何的处理类型,比如 $dbh、$sth 或 $drh。该程序返回最后调用的 dbi 方法的数据库引擎错误消息。 |
8 | $dbh->disconnect()
该例程关闭之前调用 dbi->connect() 打开的数据库连接。 |
3. 连接数据库
下面的 perl 代码显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。
#!/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";
现在,让我们来运行上面的程序,在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。保存上面代码到 sqlite.pl 文件中,并按如下所示执行。如果数据库成功创建,那么会显示下面所示的消息:
$ chmod +x sqlite.pl $ ./sqlite.pl open database successfully
4. 创建表
下面的 perl 代码段将用于在先前创建的数据库中创建一个表:
#!/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();
上述程序执行时,它会在 test.db 中创建 company 表,并显示下面所示的消息:
opened database successfully table created successfully
注意:如果您在任何操作中遇到了下面的错误: 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
在这种情况下,您已经在 dbd-sqlite 安装中打开了可用的 dbdimp.c 文件,找到 sqlite3_prepare() 函数,并把它的第三个参数 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.
5. insert 操作
下面的 perl 程序显示了如何在上面创建的 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(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();
上述程序执行时,它会在 company 表中创建给定记录,并会显示以下两行:
opened database successfully records created successfully
6. select 操作
下面的 perl 程序显示了如何从前面创建的 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();
上述程序执行时,它会产生以下结果:
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
7. update 操作
下面的 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();
上述程序执行时,它会产生以下结果:
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
8. delete 操作
下面的 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();
上述程序执行时,它会产生以下结果:
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