如何用Perl访问SQLite数据库

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

如何用Perl访问SQLite数据库

 

访问SQLite的准备

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

Debian、 Ubuntu 或者 Linux Mint

  1. <span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install sqlite3 libdbd</span><span class="pun">-</span><span class="pln">sqlite3</span><span class="pun">-</span><span class="pln">perl</span>

CentOS、 Fedora 或者 RHEL

  1. <span class="pln">$ sudo yum install sqlite perl</span><span class="pun">-</span><span class="pln">DBD</span><span class="pun">-</span><span class="typ">SQLite</span>

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

  1. <span class="com">#!/usr/bin/perl</span>
  2. <span class="kwd">my</span><span class="lit">@drv</span><span class="pun">=</span><span class="pln"> DBI</span><span class="pun">-></span><span class="pln">available_drivers</span><span class="pun">();</span>
  3. <span class="kwd">print</span><span class="pln"> join</span><span class="pun">(</span><span class="str">"\n"</span><span class="pun">,</span><span class="lit">@drv</span><span class="pun">),</span><span class="str">"\n"</span><span class="pun">;</span>

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

  1. <span class="pln">DBM</span>
  2. <span class="typ">ExampleP</span>
  3. <span class="typ">File</span>
  4. <span class="typ">Gofer</span>
  5. <span class="typ">Proxy</span>
  6. <span class="typ">SQLite</span>
  7. <span class="typ">Sponge</span>

 

Perl SQLite 访问示例

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

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

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

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

 

错误定位

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

  1. <span class="typ">Can</span><span class="str">'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.</span>
  2. <span class="str">BEGIN failed--compilation aborted at ./script.pl line 3.</span>

SQLite 的详细介绍:请点这里
SQLite 的下载地址:请点这里


via: http://xmodulo.com/access-sqlite-database-perl.html

作者:Dan Nanni 译者:geekpi 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐