如何从Perl调用MySQL存储过程?存储过程功能对于MySQL来说是相当新的功能,而MySQL模块似乎还没有跟上。
发布于 2008-09-15 17:01:52
生成数据集的MySQL存储过程需要使用Perl DBD::mysql 4.001或更高版本。(http://www.perlmonks.org/?node_id=609098)
下面是一个可以在新版本中运行的测试程序:
mysql> delimiter //
mysql> create procedure Foo(x int)
-> begin
-> select x*2;
-> end
-> //
perl -e 'use DBI; DBI->connect("dbi:mysql:database=bonk", "root", "")->prepare("call Foo(?)")->execute(21)'但是,如果您使用的DBD::mysql版本太旧,您会得到如下结果:
DBD::mysql::st execute failed: PROCEDURE bonk.Foo can't return a result set in the given context at -e line 1.您可以使用CPAN安装最新的DBD。
发布于 2008-09-15 16:27:46
发布于 2012-05-02 08:35:37
#!/usr/bin/perl
# Stored Proc - Multiple Values In, Multiple Out
use strict;
use Data::Dumper;
use DBI;
my $dbh = DBI->connect('DBI:mysql:RTPC;host=db.server.com',
'user','password',{ RaiseError => 1 }) || die "$!\n";
my $sth = $dbh->prepare('CALL storedProcedure(?,?,?,?,@a,@b);');
$sth->bind_param(1, 2);
$sth->bind_param(2, 1003);
$sth->bind_param(3, 5000);
$sth->bind_param(4, 100);
$sth->execute();
my $response = $sth->fetchrow_hashref();
print Dumper $response . "\n";我花了一段时间才弄明白,但我能够通过上面的方法得到我需要的东西。如果您需要获得多个返回“行”,我猜您只是...
while(my $response = $sth->fetchrow_hashref()) {
print Dumper $response . "\n";
}我希望它能帮上忙。
https://stackoverflow.com/questions/64200
复制相似问题