Perl对数据库的查询操作
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 "TN\tSN\tNumber\tMarque\tPastTime\n";
- 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();
循环中为使用游标读取每行数据。
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06