加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl对数据库的查询操作

发布时间:2020-12-15 21:02:53 所属栏目:大数据 来源:网络整理
导读:? ? Perl操作数据库与其他的语言操作数据库没有什么区别。 ? ? 首先,要连接数据库。 ? ? 然后,执行SQL语句。 ? ? 最后,关闭连接。 ? ? 下面为程序: ? ? test.pl ? ?? #! /usr/bin/perl# test DBI and DBD::mysqluse DBI;$dsn = "DBI:mysql:database=carn

? ? Perl操作数据库与其他的语言操作数据库没有什么区别。

? ? 首先,要连接数据库。

? ? 然后,执行SQL语句。

? ? 最后,关闭连接。


? ? 下面为程序:

? ? test.pl

? ??

#! /usr/bin/perl

# test DBI and DBD::mysql

use DBI;

$dsn = "DBI:mysql:database=carnumber;host=localhost;port=3306";

my $dbh = DBI->connect($dsn,"root","qazxsw",{'RaiseError' => 1});


my $strSQL = "select train_number,seriary_number,car_number,".
    " car_marque,past_time from trainOrder where train_number < 100";

my $sth = $dbh->prepare($strSQL);
$sth->execute();

print "TNtSNtNumbertMarquetPastTimen";
while (my $ref = $sth->fetchrow_hashref()) {
	print "$ref->{'train_number'}t".
	    "$ref->{'seriary_number'}t".
	    "$ref->{'car_number'}t".
	    "$ref->{'car_marque'}t".
	    "$ref->{'past_time'}n";
}

$sth->finish();

$dbh->disconnect();

? ? 循环中为使用游标读取每行数据。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读