简体   繁体   English

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

[英]Perl DBI with mysql: how to get return value from a stored procedure?

Does DBD::mysql implement the bind_param_inout method? 是否DBD :: mysql的实施bind_param_inout方法 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 DBD :: mysql的:: ST bind_param_inout失败:输出参数不能实现[用于声明 “呼吁spCreateTransactionRecord(?,?)” 与ParamValues:0 = NULL!1 = NULL]在./db.pl线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
)

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. 不太合适(两个数据库调用而不是一个),但是可以完成工作。

It is a known bug with "Verified" status, meaning it never got addressed. 这是一个已知的错误,状态为“已验证”,表示从未得到解决。

http://bugs.mysql.com/bug.php?id=23554 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: 单独的确认仍未解决问题的确认是, 当前(4.017)版本的源代码仍然存在错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM