如何用Perl访问SQLite数据库
SQLite是一个零配置、无服务端、基于文件的事务型数据库系统。由于它的轻量级,自包含和紧凑的设计,所以当你想要集成数据库到你的程序中时,SQLite是一个非常不错的选择。在这篇文章中,我会展示如何用Perl脚本来创建和访问SQLite数据库。我演示的Perl代码片段是完整的,所以你可以很简单地修改并集成到你的项目中。
访问SQLite的准备
我会使用SQLite DBI Perl驱动来连接到SQLite3。因此你需要在Linux中安装它(和SQLite3一起)。
Debian、 Ubuntu 或者 Linux Mint
<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
<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驱动可以通过下面的脚本访问到。
<span class="com">#!/usr/bin/perl</span>
<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>
<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>
如果你运行脚本,你应该会看见下面的输出。
<span class="pln">DBM</span>
<span class="typ">ExampleP</span>
<span class="typ">File</span>
<span class="typ">Gofer</span>
<span class="typ">Proxy</span>
<span class="typ">SQLite</span>
<span class="typ">Sponge</span>
Perl SQLite 访问示例
下面就是Perl访问SQLite的示例。这个Perl脚本会演示下面这些SQLite数据库的常规管理。
- 创建和连接SQLite数据库
- 在SQLite数据库中创建新表
- 在表中插入行
- 在表中搜索和迭代行
- 在表中更新行
- 在表中删除行
<span class="kwd">use</span><span class="pln"> DBI</span><span class="pun">;</span>
<span class="kwd">use</span><span class="pln"> strict</span><span class="pun">;</span>
<span class="com"># 定义数据库名称和驱动</span>
<span class="kwd">my</span><span class="pln"> $driver </span><span class="pun">=</span><span class="str">"SQLite"</span><span class="pun">;</span>
<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>
<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>
<span class="com"># sqlite 没有用户名密码的概念</span>
<span class="kwd">my</span><span class="pln"> $username </span><span class="pun">=</span><span class="str">""</span><span class="pun">;</span>
<span class="kwd">my</span><span class="pln"> $password </span><span class="pun">=</span><span class="str">""</span><span class="pun">;</span>
<span class="com"># 创建并连接到数据库</span>
<span class="com"># 以下创建的文件名为 xmodulo.db</span>
<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>
<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>
<span class="kwd">print</span><span class="pln"> STDERR </span><span class="str">"Database opened successfully\n"</span><span class="pun">;</span>
<span class="com"># 创建表</span>
<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>
<span class="pun">(</span><span class="pln">ID INTEGER PRIMARY KEY AUTOINCREMENT</span><span class="pun">,</span>
<span class="pln">HOSTNAME TEXT NOT NULL</span><span class="pun">,</span>
<span class="pln">IPADDRESS INT NOT NULL</span><span class="pun">,</span>
<span class="pln">OS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span>
<span class="pln">CPULOAD REAL</span><span class="pun">););</span>
<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>
<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>
<span class="kwd">print</span><span class="pln"> STDERR $DBI</span><span class="pun">::</span><span class="pln">errstr</span><span class="pun">;</span>
<span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
<span class="kwd">print</span><span class="pln"> STDERR </span><span class="str">"Table created successfully\n"</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="com"># 插入三行到表中</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span class="com"># 在表中检索行</span>
<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>
<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>
<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>
<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>
<span class="kwd">print</span><span class="pln"> STDERR $DBI</span><span class="pun">::</span><span class="pln">errstr</span><span class="pun">;</span>
<span class="pun">}</span>
<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>
<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>
<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>
<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>
<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>
<span class="pun">}</span>
<span class="com"># 更新表中的某行</span>
<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>
<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>
<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>
<span class="kwd">print</span><span class="pln"> STDERR $DBI</span><span class="pun">::</span><span class="pln">errstr</span><span class="pun">;</span>
<span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
<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>
<span class="pun">}</span>
<span class="com"># 从表中删除某行</span>
<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>
<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>
<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>
<span class="kwd">print</span><span class="pln"> STDERR $DBI</span><span class="pun">::</span><span class="pln">errstr</span><span class="pun">;</span>
<span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span>
<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>
<span class="pun">}</span>
<span class="com"># 断开数据库连接</span>
<span class="pln">$dbh</span><span class="pun">-></span><span class="pln">disconnect</span><span class="pun">();</span>
<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”的数据库文件,并会有下面的输出。
<span class="typ">Database</span><span class="pln"> opened successfully</span>
<span class="typ">Table</span><span class="pln"> created successfully</span>
<span class="pln">ID</span><span class="pun">:</span><span class="lit">1</span>
<span class="pln">HOSTNAME</span><span class="pun">:</span><span class="pln"> xmodulo</span>
<span class="pln">OS</span><span class="pun">:</span><span class="typ">Ubuntu</span><span class="lit">14.10</span>
<span class="pln">CPULOAD</span><span class="pun">:</span><span class="lit">0</span>
<span class="pln">ID</span><span class="pun">:</span><span class="lit">2</span>
<span class="pln">HOSTNAME</span><span class="pun">:</span><span class="pln"> bert</span>
<span class="pln">OS</span><span class="pun">:</span><span class="typ">CentOS</span><span class="lit">7</span>
<span class="pln">CPULOAD</span><span class="pun">:</span><span class="lit">0</span>
<span class="pln">ID</span><span class="pun">:</span><span class="lit">3</span>
<span class="pln">HOSTNAME</span><span class="pun">:</span><span class="pln"> puppy</span>
<span class="pln">OS</span><span class="pun">:</span><span class="typ">Ubuntu</span><span class="lit">14.10</span>
<span class="pln">CPULOAD</span><span class="pun">:</span><span class="lit">0</span>
<span class="pln">A total of </span><span class="lit">2</span><span class="pln"> rows updated</span>
<span class="pln">A total of </span><span class="lit">1</span><span class="pln"> rows deleted</span>
<span class="typ">Exit</span><span class="pln"> the database</span>
错误定位
如果你尝试没有安装SQLite DBI驱动的情况下使用Perl访问SQLite的话,你会遇到下面的错误。你必须按开始说的安装DBI驱动。
<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>
<span class="str">BEGIN failed--compilation aborted at ./script.pl line 3.</span>
SQLite 的详细介绍:请点这里
SQLite 的下载地址:请点这里
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06