网上很多资料都是PHP,JAVA如何调用MYSQL存储过程的
而PERL这方面的资料确很少
本人通过实践,终于解决了如何在PERL下使用DBI调用MYSQL的存储过程
我们更有意义升级MYSQL到5.0以上版本了!
要想使用MYSQL的存储过程你的MYSQL版本必须为5.0以上(众人:废话,讲正题)
好好,下面的才是真正重要的。
不过讲之前喝口水先(众人:。。。。。。。。。。)
好了,现在水喝完了。
现在你得检查下你的DBI模块安装了没?没装就快去装,怎么装我就不说了
然后检测你的DBD::mysql模块是不是3.0002_5或者以上版本,这里有一点要注意3.0002_后面的数字,如果小于5,通常情况下用V能看到的只是3.0002,如果你不能确人你的版本,那就去装最新版本的就OK了。在ubuntu下新立得软件包自带的是3.0002_2,就因为这个害得我白费了一上午都没成功。
目前DBD::mysql最新版本是4.003,下载下来后直接安装。
不过也许你会遇见下面的信息
Cannot find the file 'mysql_config'! Your execution PATH doesn't seem
not contain the path to mysql_config. Resorting to guessed values!
Can't exec "mysql_config": No such file or directory at Makefile.PL line 465.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 465.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 465.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 465.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 465.
mysql_config是什么玩意?
其实这个就是mysql的一个工具,不过通常情况下不会和mysql一起安装,而需要自己去安装。
当然如果你安装了mysql的开发包(mysql-devel)就不会有这个问题了。所以建议你(虽然说强迫你更合适些)把mysql的开发包也安装了吧。
在Ubuntu下是个叫libmysqlclient15-dev的包,装好后mysql_config也就装上了
现在在安装试下,应该就能通过了
如果没有问题了就该进行下测试了
我们先来个存储过程
$dbh->do("drop procedure if exists someproc") or print $DBI::errstr;$dbh->do("create procedure somproc() deterministic
begin
declare a,b,c,d int;
set a=1;
set b=2;
set c=3;
set d=4;
select a, b, c, d;
select d, c, b, a;
select b, a, c, d;
select c, b, d, a;
end") or print $DBI::errstr;
什么意思我就不解释了,不懂的自己去看MYSQL存储过程去(众人:KAO,牛X什么!)
测试下这个存储过程
在mysql中的运行结果
mysql> call somproc()$$
+------+------+------+------+
| a | b | c | d |
+------+------+------+------+
| 1 | 2 | 3 | 4 |
+------+------+------+------+
1 row in set (0.01 sec)
+------+------+------+------+
| d | c | b | a |
+------+------+------+------+
| 4 | 3 | 2 | 1 |
+------+------+------+------+
1 row in set (0.01 sec)
+------+------+------+------+
| b | a | c | d |
+------+------+------+------+
| 2 | 1 | 3 | 4 |
+------+------+------+------+
1 row in set (0.01 sec)
+------+------+------+------+
| c | b | d | a |
+------+------+------+------+
| 3 | 2 | 4 | 1 |
+------+------+------+------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
mysql> call somproc();
+------+------+------+------+
| a | b | c | d |
+------+------+------+------+
| 1 | 2 | 3 | 4 |
+------+------+------+------+
1 row in set (0.00 sec)
+------+------+------+------+
| d | c | b | a |
+------+------+------+------+
| 4 | 3 | 2 | 1 |
+------+------+------+------+
1 row in set (0.00 sec)
+------+------+------+------+
| b | a | c | d |
+------+------+------+------+
| 2 | 1 | 3 | 4 |
+------+------+------+------+
1 row in set (0.00 sec)
+------+------+------+------+
| c | b | d | a |
+------+------+------+------+
| 3 | 2 | 4 | 1 |
+------+------+------+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
女士们,先生们,我的MSN是[email protected],大家记好了啊!现在让我们体会激动人心的时刻吧。
$sth=$dbh->prepare('call someproc()') ||
die $DBI::err.": ".$DBI::errstr;$sth->execute || die DBI::err.": ".$DBI::errstr; $rowset=0;
do {
print "\nRowset ".++$i."\n---------------------------------------\n\n";
foreach $colno (0..$sth->{NUM_OF_FIELDS}) {
print $sth->{NAME}->[$colno]."\t";
}
print "\n";
while (@row= $sth->fetchrow_array()) {
foreach $field (0..$#row) {
print $row[$field]."\t";
}
print "\n";
}
} until (!$sth->more_results);
运行结果
Rowset 1
---------------------------------------
a b c d
1 2 3 4
Rowset 2
---------------------------------------
d c b a
4 3 2 1
Rowset 3
---------------------------------------
b a c d
2 1 3 4
Rowset 4
---------------------------------------
c b d a
3 2 4 1
至此全部完成,请大家鼓掌!
???怎么没声呢??哎,人,人呢,怎么都没了??