Perl Mysql数据库操作实现代码
程序员文章站
2022-06-28 23:32:35
一. 安装dbi模块 步骤1: 从tools栏目中下载dbi.zip,下载完后用winzip解开到一个temp目录,共有三个文件: readme dbi.ppd dbi.t...
一. 安装dbi模块
步骤1:
从tools栏目中下载dbi.zip,下载完后用winzip解开到一个temp目录,共有三个文件:
readme
dbi.ppd
dbi.tar.gz
步骤2:
在dos窗口下,temp目录中运行下面的dos命令:
ppm install dbi.ppd
如果提示无效命令,可在perl/bin目录下运行
二. 安装dbd-mysql模块
从软件下载中下载dbd-mysql.zip,安装方法同一.
三. 准备数据库
启动mysql,首先创建一个数据库mydata,然后创建一个表address
mysql> create database mydata;
query ok, 1 row affected (0.00 sec)
mysql> use mydata;
database changed
mysql> create table address (
-> id int(5) not null,
-> name varchar(40) not null,
-> email varchar(50) not null,
-> telephone int(12) null);
query ok, 0 rows affected (0.05 sec)
输入些数据:
mysql> insert into address values (
-> 1,'nighthawk','nighthawk@163.net',92384092);
query ok, 1 row affected (0.00 sec)
四. 下面用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;
nighthawk
步骤1:
从tools栏目中下载dbi.zip,下载完后用winzip解开到一个temp目录,共有三个文件:
readme
dbi.ppd
dbi.tar.gz
步骤2:
在dos窗口下,temp目录中运行下面的dos命令:
ppm install dbi.ppd
如果提示无效命令,可在perl/bin目录下运行
二. 安装dbd-mysql模块
从软件下载中下载dbd-mysql.zip,安装方法同一.
三. 准备数据库
启动mysql,首先创建一个数据库mydata,然后创建一个表address
mysql> create database mydata;
query ok, 1 row affected (0.00 sec)
mysql> use mydata;
database changed
mysql> create table address (
-> id int(5) not null,
-> name varchar(40) not null,
-> email varchar(50) not null,
-> telephone int(12) null);
query ok, 0 rows affected (0.05 sec)
输入些数据:
mysql> insert into address values (
-> 1,'nighthawk','nighthawk@163.net',92384092);
query ok, 1 row affected (0.00 sec)
四. 下面用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;
nighthawk
下一篇: Perl 获取shell命令的执行结果