perl中使用数据库

前端之家收集整理的这篇文章主要介绍了perl中使用数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 连接数据库

sub connect_MysqL

{
my   $dbh;
my   %info = (dbname=>"asteriskcdrdb",
              dbhost=>"localhost",
              dbuser=>"root",
              dbpasswd=>"passw0rd"
              );
      $dbh = DBI->connect("DBI:MysqL:database=库名;host=localhost","用户名","密码")or die "Can't Connect Database Server: $!";
      return $dbh;

}

2. 搜索数据(变量都需要声明)

$DBH=&connect_MysqL();

$sth=$DBH->prepare(select * from 表名 where  条件);

$sth->execute or $DBH->err;

$row=$sth->fetchrow_hashref();

$sth->finish;

 

3. 插入数据

$DBH=&connect_MysqL();

$DBH->do("insert into 表名 set  字段=值 ");


4.删除数据

$DBH=&connect_MysqL();

$DBH->do("delete *  from 表名 where 条件");


5.更改数据

$DBH=&connect_MysqL();

$DBH->do("update 表名 set  字段=值  where 条件");

猜你在找的Perl相关文章