perl mysql 操作

先安装模块

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;


你可能感兴趣的:(mysql,user,perl,database,query,email)