perl mysql 操作

来源:互联网 发布:java中boolean默认值 编辑:程序博客网 时间:2024/04/29 19:46

先安装模块

DBI

模块安装方法:http://blog.csdn.net/ajaxchen_615/article/details/7489294

#!/usr/bin/perl -w

use strict;
use warnings;
use DBI;

my $database = 'dilicms';
my $user = 'root';
my $passwd = '';

#查询数据
my $dbh = DBI->connect("DBI:mysql:$database", $user, $passwd);
my $statement = 'select `uid`,`username`,`password`,`email` from `dili_admins`';
my $sth = $dbh->prepare($statement) or die "Can't prepare $statement:$dbh->errstr\n";
my $rv = $sth->execute or die "Can't execute the query:$sth->errstr\n";

#更新数据
my $statement_update = "update `dili_admins` set `username`='rootroot' where `uid` = 1";
my $rv = $dbh->do($statement_update);
###################################################

# print $rv;
# while ( my @row = $sth->fetchrow_array) {
#  print $row[0],"\n";
# }
#遍历
while ( my $hash_ref = $sth->fetchrow_hashref ) {
print $hash_ref->{username},"\n";
}

$sth->finish;
$dbh->disconnect;


原创粉丝点击