如何使用perl 5.18 连接mysql 数据库
1个回答
展开全部
用DBI就可以给你段代码:
use DBI;
my $dbname = "test";
my $location = "192.168.1.115";
my $port = "3306"; #这是mysql的缺省
my $database = "DBI:mysql:$dbname:$location:$port";
my $db_user = "root";
my $db_pass = "123456";
my $dbh = DBI->connect($database,$db_user,$db_pass);
my $sql = "SELECT * FROM tablename";
my $sth = $dbh->prepare($sql);#准备
$sth->execute() or die "无法执行SQL语句:$dbh->errstr"; #执行
while (@data = $sth->fetchrow_array()) {#fetchrow_array返回row
print "Email:$data[0]\t Telephone:$data[1]\n";
}
$sth->finish();
$dbh->disconnect;#断开数据库连接
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |