【问题标题】:How do I call MySQL stored procedures from Perl?如何从 Perl 调用 MySQL 存储过程?
【发布时间】:2010-09-09 00:23:48
【问题描述】:

如何从 Perl 调用 MySQL 存储过程?存储过程功能对 MySQL 来说是相当新的,Perl 的 MySQL 模块似乎还没有跟上。

【问题讨论】:

    标签: mysql perl stored-procedures


    【解决方案1】:

    生成数据集的 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。

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:
        #!/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";
        }
        

        希望对你有帮助。

        【讨论】:

          【解决方案4】:

          首先,您可能应该通过 DBI 库进行连接,然后您应该使用绑定变量。例如。类似:

          #!/usr/bin/perl
          #
          use strict;
          use DBI qw(:sql_types);
          
          my $dbh = DBI->connect(
                      $ConnStr,
                      $User,
                      $Password,
                      {RaiseError => 1, AutoCommit => 0}
                    ) || die "Database connection not made: $DBI::errstr";
          my $sql = qq {CALL someProcedure(1);}    } 
          
          my $sth = $dbh->prepare($sql);
          eval {
            $sth->bind_param(1, $argument, SQL_VARCHAR);
          };
          if ($@) {
           warn "Database error: $DBI::errstr\n";
           $dbh->rollback(); #just die if rollback is failing
          }
          
          $dbh->commit();
          

          请注意,我尚未对此进行测试,您必须在 CPAN 上查找确切的语法。

          【讨论】:

          • 您忘记了 eval {} 后的分号。这是一个常见的错误。
          【解决方案5】:

          您好,与上面类似,但使用的是 SQL exec。我无法让 CALL 命令工作。您需要填写方括号内的任何内容并删除方括号。

          使用 DBI; #START:设置数据库并连接 我的 $host = '*[服务器]*\\*[数据库]*'; 我的 $database = '*[table]*'; 我的 $user = '*[user]*'; 我的 $auth = '*[密码]*'; 我的 $dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$database"; 我的 $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 }); #END : 设置数据库并连接 $sql = "exec *[存储过程名称]* *[param1]*,*[param2]*,*[param3]*;"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL 错误: $DBI::errstr\n";

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-02-20
            • 2012-12-30
            • 2013-10-07
            • 1970-01-01
            • 2011-05-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多