perl连接Sqlite数据库的例子

发布时间:2020-02-01编辑:脚本学堂
perl连接Sqlite数据库的例子

perl连接sqlite数据库的例子,供大家学习参考。
 

复制代码 代码如下:

#!/usr/bin/perl

use DBI; # perl用以操作sqlite的模块,有这一个模块就足够了
use strict; # 初学必须加上这一句,以严格要求语句的撰写
use warnings;

main:
{
    my $dbargs = {AutoCommit => 0,  #使用事件
                  PrintError => 1};

# 连接到数据库
my $dbh = DBI->connect("dbi:SQLite:dbname=test.db","","",$dbargs);

# 创建表
$dbh->do("create table test(id int primary key, age, name)");                

# 插入数据
$dbh->do("insert into test values(Null,'34','Liu Qiang'");
  
#更新数据
$dbh->do("update test set age='33' where name='Liu Qiang'");

#删除数据
$dbh->do("delete from test where name='Liu Qiang'");
   
#查询数据
my $sql = "SELECT * FROM test";
my $dbconn = $dbh->prepare($sql);
$dbconn->execute();

my (@row_ary,$cc,$bb,$dd);
while (@row_ary = $dbconn->fetchrow_array ){
  my($cc,$bb,$dd) = @row_ary;
   print "表Test的内容为n";
   print "t@row_aryn";
}
#删除表
#$dbh->do("drop table test");

#清空表,表结构还存在
$dbh->do("delete from test");

 if ($dbh->err()) { die "$DBI::errstrn"; } #连接错误提示
    $dbh->commit();
    $dbh->disconnect(); #断开数据库链接
}