用perl訪問mysql數(shù)據(jù)庫之二
發(fā)表時間:2024-06-12 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]四. 下面用perl程序來插入若干記錄并做查詢. use DBI; #連接數(shù)據(jù)庫mydata my $dbh = DBI->connect(’DBI:mysql:mydata’) or die "無法連接數(shù)據(jù)庫: " . DBI->errstr; print &qu...
四. 下面用perl程序來插入若干記錄并做查詢.
use DBI;
#連接數(shù)據(jù)庫mydata
my $dbh = DBI->connect(’DBI:mysql:mydata’) or die "無法連接數(shù)據(jù)庫: " . DBI->errstr;
print "插入若干記錄n";
my $sth = $dbh->prepare(q{
INSERT INTO address (id, name,email,telephone) VALUES (?, ?, ?, ?)
}) });
print "輸入記錄,回車結(jié)束:";
while ($inputdata =<>) {
chop $inputdata;
last unless($inputdata);
my ($id, $name,$email, $tel) = split( /,/, $inputdata);
$sth->execute($id, $name, $email,$tel)
}
# $dbh->commit;
print "下面根據(jù)輸入的名字打印出EMAIL地址和電話n";
my $sth = $dbh->prepare(’SELECT * FROM address WHERE name=?’)
or die $dbh->errstr;
print "請輸入姓名,回車結(jié)束:";
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;