用perl访问mysql数据库之二
发表时间:2024-02-09 来源:明辉站整理相关软件相关文章人气:
[摘要]四. 下面用perl程序来插入若干记录并做查询. use DBI; #连接数据库mydata my $dbh = DBI->connect(’DBI:mysql:mydata’) or die "无法连接数据库: " . DBI->errstr; print &qu...
四. 下面用perl程序来插入若干记录并做查询.
use DBI;
#连接数据库mydata
my $dbh = DBI->connect(’DBI:mysql:mydata’) or die "无法连接数据库: " . DBI->errstr;
print "插入若干记录n";
my $sth = $dbh->prepare(q{
INSERT INTO address (id, name,email,telephone) VALUES (?, ?, ?, ?)
}) });
print "输入记录,回车结束:";
while ($inputdata =<>) {
chop $inputdata;
last unless($inputdata);
my ($id, $name,$email, $tel) = split( /,/, $inputdata);
$sth->execute($id, $name, $email,$tel)
}
# $dbh->commit;
print "下面根据输入的名字打印出EMAIL地址和电话n";
my $sth = $dbh->prepare(’SELECT * FROM address WHERE name=?’)
or die $dbh->errstr;
print "请输入姓名,回车结束:";
while ($inputname =<>) {
my @data;
chomp $inputname;
last unless($inputname);
$sth->execute($inputname) or die "错误: " . $sth->errstr;
while (@data = $sth->fetchrow_array()) {
print "Email:$data[2]t Telephone:$data[3]n";
}
}
#断开连接
$dbh->disconnect;