Perl DBI 执行不维护 MySQL 存储过程结果

发布于 2024-08-27 00:10:58 字数 1655 浏览 6 评论 0原文

我在从 Perl 执行存储过程(使用 DBI 模块)时遇到问题。如果我执行一个简单的 SELECT * FROM table 就没有问题。

SQL 代码是:

    DROP FUNCTION IF EXISTS update_current_stock_price;
 DELIMITER |
  CREATE FUNCTION update_current_stock_price (symbolIN VARCHAR(20), nameIN VARCHAR(150), currentPriceIN DECIMAL(10,2), currentPriceTimeIN DATETIME)
   RETURNS INT
   DETERMINISTIC
    BEGIN
  DECLARE outID INT;
  SELECT `id` INTO outID FROM `mydb449`.`app_stocks` WHERE `symbol` = symbolIN;

  IF outID > 0 THEN
   UPDATE `mydb449`.`app_stocks`
   SET `currentPrice` = currentPriceIN, `currentPriceTime` = currentPriceTimeIN
   WHERE `id` = outID;

  ELSE
   INSERT INTO `mydb449`.`app_stocks` 
   (`symbol`, `name`, `currentPrice`, `currentPriceTime`) 
   VALUES (symbolIN, nameIN, currentPriceIN, currentPriceTimeIN);
   SELECT LAST_INSERT_ID() INTO outID;
  END IF;

  RETURN outID;
    END|
 DELIMITER ;

Perl 代码:

 $sql = "select update_current_stock_price('$csv_result[0]', '$csv_result[1]', '$csv_result[2]',  '$currentDateTime') as `id`;";
 My::Extra::StandardLog("SQL being used: ".$sql);
 my $query_handle = $dbh->prepare($sql);
 $query_handle->execute();
 $query_handle->bind_columns(\$returnID);
 $query_handle->fetch();

如果我执行 select update_current_stock_price('aapl', 'Apple Corp', '264.4', '2010-03-17 00:00:00') asid ; 使用 mysql CLI 客户端正确执行存储的函数并返回现有 ID 或新 ID。

然而,Perl 只会返回一个新的 ID(每次运行时加 1)。它也不将结果存储在数据库中。看起来它在 update_current_stock_price 函数运行后立即对新 id 执行 DELETE

有什么帮助吗? Perl 对程序做了什么我应该知道的奇怪的事情吗?

在你问之前,抱歉,我无权访问二进制日志记录。

I'm having a problem with executing a stored procedure from Perl (using the DBI Module). If I execute a simple SELECT * FROM table there are no problems.

The SQL code is:

    DROP FUNCTION IF EXISTS update_current_stock_price;
 DELIMITER |
  CREATE FUNCTION update_current_stock_price (symbolIN VARCHAR(20), nameIN VARCHAR(150), currentPriceIN DECIMAL(10,2), currentPriceTimeIN DATETIME)
   RETURNS INT
   DETERMINISTIC
    BEGIN
  DECLARE outID INT;
  SELECT `id` INTO outID FROM `mydb449`.`app_stocks` WHERE `symbol` = symbolIN;

  IF outID > 0 THEN
   UPDATE `mydb449`.`app_stocks`
   SET `currentPrice` = currentPriceIN, `currentPriceTime` = currentPriceTimeIN
   WHERE `id` = outID;

  ELSE
   INSERT INTO `mydb449`.`app_stocks` 
   (`symbol`, `name`, `currentPrice`, `currentPriceTime`) 
   VALUES (symbolIN, nameIN, currentPriceIN, currentPriceTimeIN);
   SELECT LAST_INSERT_ID() INTO outID;
  END IF;

  RETURN outID;
    END|
 DELIMITER ;

The Perl code:

 $sql = "select update_current_stock_price('$csv_result[0]', '$csv_result[1]', '$csv_result[2]',  '$currentDateTime') as `id`;";
 My::Extra::StandardLog("SQL being used: ".$sql);
 my $query_handle = $dbh->prepare($sql);
 $query_handle->execute();
 $query_handle->bind_columns(\$returnID);
 $query_handle->fetch();

If I execute select update_current_stock_price('aapl', 'Apple Corp', '264.4', '2010-03-17 00:00:00') asid; using the mysql CLI client it executes the stored function correctly and returns an existing ID, or the new ID.

However, the Perl will only return a new ID, (incrementing by 1 on each run). It also doesn't store the result in the database. It looks like it's executing a DELETE on the new id just after the update_current_stock_price function is run.

Any help? Does Perl do anything funky to procedures I should know about?

Before you ask, I don't have access to binary logging, sorry.

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

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

发布评论

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

评论(3

请远离我 2024-09-03 00:10:58

也许您在事务中执行此操作并且它正在回滚?该行已插入,但从未提交且不可见。

如果有疑问,我会在您的开发服务器上尝试并启用常规查询日志。

另外,您可能想了解 INSERT ... ON DUPLICATE KEY UPDATE 语法,它可能可以完成您想要做的事情。

Perhaps you're doing it in a transaction and it's getting rolled back? The row is inserted but never becomes committed and cannot be seen.

I'd try it on your dev server and enable general query log, if in doubt.

Also you may want to know about the INSERT ... ON DUPLICATE KEY UPDATE syntax, which can probably do what you're trying to do anyway.

剩余の解释 2024-09-03 00:10:58

在bind_columns调用之前尝试

$query_handle->dump_results(15, "\n", '|');

看看它是否真的得到了结果,您也可以尝试将SELECT storeprocedure替换为SELECT * FROM storeprocedure

try

$query_handle->dump_results(15, "\n", '|');

before the bind_columns call to see if it is actually getting the results back, you could also try replace SELECT storedprocedure with SELECT * FROM storedprocedure

2024-09-03 00:10:58

您应该检查您是否正在运行最新版本的 DBD::mysql(这是 DBI 使用的 MySQL 驱动程序)。存储过程曾经存在几个问题,至少有一些在最近的版本中得到了修复。也许这些资源也很有帮助:

You should check that you are running the latest version of DBD::mysql (which is the MySQL-driver used by DBI). There used to be several issues with stored procedures, at least some are fixed in recent versions. Maybe these ressources are also helpful:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文