如何用Perl访问SQLite数据库

SQLite是一个零配置、无服务端、基于文件的事务型数据库系统。由于它的轻量级,自包含和紧凑的设计,所以当你想要集成数据库到你的程序中时,SQLite是一个非常不错的选择。在这篇文章中,我会展示如何用Perl脚本来创建和访问SQLite数据库。我演示的Perl代码片段是完整的,所以你可以很简单地修改并集成到你的项目中。

访问SQLite的准备

我会使用SQLite DBI Perl驱动来连接到SQLite3。因此你需要在Linux中安装它(和SQLite3一起)。

Debian、 Ubuntu 或者 Linux Mint

 
 
  1. $ sudo apt-get install sqlite3 libdbd-sqlite3-perl

CentOS、 Fedora 或者 RHEL

 
 
  1. $ sudo yum install sqlite perl-DBD-SQLite

安装后,你可以检查SQLite驱动可以通过下面的脚本访问到。

 
 
  1. #!/usr/bin/perl
  2. my @drv = DBI->available_drivers();
  3. print join("\n", @drv), "\n";

如果你运行脚本,你应该会看见下面的输出。

 
 
  1. DBM
  2. ExampleP
  3. File
  4. Gofer
  5. Proxy
  6. SQLite
  7. Sponge

Perl SQLite 访问示例

下面就是Perl访问SQLite的示例。这个Perl脚本会演示下面这些SQLite数据库的常规管理。

  • 创建和连接SQLite数据库
  • 在SQLite数据库中创建新表
  • 在表中插入行
  • 在表中搜索和迭代行
  • 在表中更新行
  • 在表中删除行
 
 
  1. use DBI;
  2. use strict;
  3. # 定义数据库名称和驱动
  4. my $driver = "SQLite";
  5. my $db_name = "xmodulo.db";
  6. my $dbd = "DBI:$driver:dbname=$db_name";
  7. # sqlite 没有用户名密码的概念
  8. my $username = "";
  9. my $password = "";
  10. # 创建并连接到数据库
  11. # 以下创建的文件名为 xmodulo.db
  12. my $dbh = DBI->connect($dbd, $username, $password, { RaiseError => 1 })
  13. or die $DBI::errstr;
  14. print STDERR "Database opened successfully\n";
  15. # 创建表
  16. my $stmt = qq(CREATE TABLE IF NOT EXISTS NETWORK
  17. (ID INTEGER PRIMARY KEY AUTOINCREMENT,
  18. HOSTNAME TEXT NOT NULL,
  19. IPADDRESS INT NOT NULL,
  20. OS CHAR(50),
  21. CPULOAD REAL););
  22. my $ret = $dbh->do($stmt);
  23. if($ret < 0) {
  24. print STDERR $DBI::errstr;
  25. } else {
  26. print STDERR "Table created successfully\n";
  27. }
  28. # 插入三行到表中
  29. $stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)
  30. VALUES ('xmodulo', 16843009, 'Ubuntu 14.10', 0.0));
  31. $ret = $dbh->do($stmt) or die $DBI::errstr;
  32. $stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)
  33. VALUES ('bert', 16843010, 'CentOS 7', 0.0));
  34. $ret = $dbh->do($stmt) or die $DBI::errstr;
  35. $stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD)
  36. VALUES ('puppy', 16843011, 'Ubuntu 14.10', 0.0));
  37. $ret = $dbh->do($stmt) or die $DBI::errstr;
  38. # 在表中检索行
  39. $stmt = qq(SELECT id, hostname, os, cpuload from NETWORK;);
  40. my $obj = $dbh->prepare($stmt);
  41. $ret = $obj->execute() or die $DBI::errstr;
  42. if($ret < 0) {
  43. print STDERR $DBI::errstr;
  44. }
  45. while(my @row = $obj->fetchrow_array()) {
  46. print "ID: ". $row[0] . "\n";
  47. print "HOSTNAME: ". $row[1] ."\n";
  48. print "OS: ". $row[2] ."\n";
  49. print "CPULOAD: ". $row[3] ."\n\n";
  50. }
  51. # 更新表中的某行
  52. $stmt = qq(UPDATE NETWORK set CPULOAD = 50 where OS='Ubuntu 14.10';);
  53. $ret = $dbh->do($stmt) or die $DBI::errstr;
  54. if( $ret < 0 ) {
  55. print STDERR $DBI::errstr;
  56. } else {
  57. print STDERR "A total of $ret rows updated\n";
  58. }
  59. # 从表中删除某行
  60. $stmt = qq(DELETE from NETWORK where ID=2;);
  61. $ret = $dbh->do($stmt) or die $DBI::errstr;
  62. if($ret < 0) {
  63. print STDERR $DBI::errstr;
  64. } else {
  65. print STDERR "A total of $ret rows deleted\n";
  66. }
  67. # 断开数据库连接
  68. $dbh->disconnect();
  69. print STDERR "Exit the database\n";

上面的Perl脚本运行成功后会创建一个叫“xmodulo.db”的数据库文件,并会有下面的输出。

 
 
  1. Database opened successfully
  2. Table created successfully
  3. ID: 1
  4. HOSTNAME: xmodulo
  5. OS: Ubuntu 14.10
  6. CPULOAD: 0
  7. ID: 2
  8. HOSTNAME: bert
  9. OS: CentOS 7
  10. CPULOAD: 0
  11. ID: 3
  12. HOSTNAME: puppy
  13. OS: Ubuntu 14.10
  14. CPULOAD: 0
  15. A total of 2 rows updated
  16. A total of 1 rows deleted
  17. Exit the database

错误定位

如果你尝试没有安装SQLite DBI驱动的情况下使用Perl访问SQLite的话,你会遇到下面的错误。你必须按开始说的安装DBI驱动。

 
 
  1. 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.
  2. BEGIN failed--compilation aborted at ./script.pl line 3.


原文发布时间为:2015-07-06

本文来自云栖社区合作伙伴“Linux中国”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值