linux下perl操作mysql数据库_MySQL
程序员文章站
2024-01-25 21:14:46
...
bitsCN.com
use DBI;
my $driver="DBI:mysql";
my $database="perl_test";
my $user="root";
my $host="localhost";
my $passwd="root";
my $rules="alert_rules";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user;password=$passwd")
or die "Can't connect: " . DBI->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
# 打印获取的数据
while(@recs=$sth->fetchrow_array){
print $recs[0].":".$recs[1]."/n";
}
$sth->finish();
$dbh->disconnect();
假定 以安装好perl语言和mysql数据库。
DBI安装:DBI详细信息参考:http://dbi.perl.org/
1.下载DBI包:
wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.620.tar.gz
2 解压
tar xzf DBI-1.620.tar.gz
3 进入解压包内,
perl Makefile.PL
4 make test
5 make
6 make install (如果不是root用户,要sudo一下)
DBD安装:
1.下载DBD
wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.021.tar.gz
2. 3. 同上
在进行第三步是可能会出现mysql_config找不到的情况,应该是没有安装开发工具包
sudo apt-get install libmysqld-dev
sudo apt-get install libmysqlclient-dev
来安装这两个包。
其余同上。
完成安装后就可以利用perl脚本进行mysql数据的操作了。
编写perl脚本操作数据库
#! /usr/bin/perl
use DBI;
my $driver="DBI:mysql";
my $database="perl_test";
my $user="root";
my $host="localhost";
my $passwd="root";
my $rules="alert_rules";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user;password=$passwd")
or die "Can't connect: " . DBI->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
# 打印获取的数据
while(@recs=$sth->fetchrow_array){
print $recs[0].":".$recs[1]."/n";
}
$sth->finish();
$dbh->disconnect();
bitsCN.com
推荐阅读
-
linux下perl操作mysql数据库_MySQL
-
远程Linux服务器修改Mysql数据库编码及解决编码修改后Mysql无法_MySQL
-
Windows2003 下 MySQL 数据库每天自动备份
-
Linux系统下安装MySQL的步骤详解
-
大侠,在windows环境下的mysql 存储过程、视图在linux下是否都能运行
-
Linux 下 MySQL 5.5.8 源码编译安装记录_MySQL
-
Linux自动备份MySQL数据库的实用方法
-
Linux Ext3下删除MySQL数据库的数据恢复案例
-
Linux下C语言连接MySQL
-
linux系统下实现mysql热备份详细步骤(mysql主从复制)_MySQL