Perl DBI 与 mysql:如何从存储过程获取返回值?

发布于 2024-09-17 10:05:06 字数 1177 浏览 10 评论 0原文

DBD::mysql 是否实现了 bind_param_inout 方法? 我在尝试时收到以下错误消息:

DBD::mysql::st bind_param_inout 失败:输出参数不正确 实施了[for声明“调用 spCreateTransactionRecord(?, ?)" 与 ParamValues: 0=Null!, 1=Null!] at ./db.pl第23行

我的代码:

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBI qw(:sql_types);
use Data::Dumper;

my ($dbh, $dsn, $sth, $sql);
my ($RecID, TypeID);
my ($user, $pass) = '';

# Open DB connection
$dsn = "dbi:mysql:database=mp;mysql_read_default_file=$ENV{HOME}/.my.cnf";
$dbh = DBI->connect($dsn, $user, $pass, 
             {RaiseError=>1, AutoCommit=>0, ShowErrorStatement=>1}) 
            || die "DB open error: $DBI::errstr";

# Call stored procedure
$sql = "call spCreateTransactionRecord(?, ?)";
$sth = $dbh->prepare($sql);
$sth->bind_param_inout(2, \$p_RecID, 11, {TYPE=>SQL_INTEGER});
$sth->execute($p_TypeID) || print $sth->errstr;

# Disconnects
$dbh->commit();
$dbh->disconnect;

存储过程声明为:

CREATE PROCEDURE spCreateTransactionRecord (
    IN  p_TypeID INTEGER,
    OUT p_RecID  INTEGER
)

Does DBD::mysql implement the bind_param_inout method?
I am getting the following error messages when trying it out:

DBD::mysql::st bind_param_inout
failed: Output parameters not
implemented [for Statement "call
spCreateTransactionRecord(?, ?)" with
ParamValues: 0=Null!, 1=Null!] at
./db.pl line 23

My code:

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBI qw(:sql_types);
use Data::Dumper;

my ($dbh, $dsn, $sth, $sql);
my ($RecID, TypeID);
my ($user, $pass) = '';

# Open DB connection
$dsn = "dbi:mysql:database=mp;mysql_read_default_file=$ENV{HOME}/.my.cnf";
$dbh = DBI->connect($dsn, $user, $pass, 
             {RaiseError=>1, AutoCommit=>0, ShowErrorStatement=>1}) 
            || die "DB open error: $DBI::errstr";

# Call stored procedure
$sql = "call spCreateTransactionRecord(?, ?)";
$sth = $dbh->prepare($sql);
$sth->bind_param_inout(2, \$p_RecID, 11, {TYPE=>SQL_INTEGER});
$sth->execute($p_TypeID) || print $sth->errstr;

# Disconnects
$dbh->commit();
$dbh->disconnect;

The stored procedures is declared as:

CREATE PROCEDURE spCreateTransactionRecord (
    IN  p_TypeID INTEGER,
    OUT p_RecID  INTEGER
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-09-24 10:05:06

新代码有一种解决方法:

# Call stored procedure
$sql = "call spCreateTransactionRecord($p_TypeID, \@rtnVal)";
$dbh->do($sql);
$p_RecID = $dbh->selectrow_array('SELECT @rtnVal');
print "Received RecID = $p_RecID\n";

不太合适(两次数据库调用而不是一次),但可以完成工作。

The new code with a workaround:

# Call stored procedure
$sql = "call spCreateTransactionRecord($p_TypeID, \@rtnVal)";
$dbh->do($sql);
$p_RecID = $dbh->selectrow_array('SELECT @rtnVal');
print "Received RecID = $p_RecID\n";

Not as proper (two database calls instead of one) but does the job.

画尸师 2024-09-24 10:05:06

这是一个具有“已验证”状态的已知错误,这意味着它从未得到解决。

http://bugs.mysql.com/bug.php?id=23554

该错误报告还包含可能的解决方法。

另一个确认该问题仍未得到解决的消息是 源当前(4.017)版本的代码仍然有错误:

if (is_inout)
{
   do_error(sth, JW_ERR_NOT_IMPLEMENTED, "Output parameters not implemented", NULL);
   return FALSE;
}

It is a known bug with "Verified" status, meaning it never got addressed.

http://bugs.mysql.com/bug.php?id=23554

That bug reports also contains a possible workaround.

A separate confirmation that the issue is still not addressed is that the source code for the current (4.017) version still has the error:

if (is_inout)
{
   do_error(sth, JW_ERR_NOT_IMPLEMENTED, "Output parameters not implemented", NULL);
   return FALSE;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文