MySQL存储过程有时会返回0行

时间:2023-02-02 16:38:14

EDIT: I've now tried pyodbc as well as pymysql, and have the same result (zero rows returned when calling a stored procedure). Forgot to mention before that this is on Ubuntu 16.04.2 LTS using the MySQL ODBC 5.3 Driver (libmyodbc5w.so).

编辑:我现在尝试了pyodbc以及pymysql,并且具有相同的结果(调用存储过程时返回零行)。之前忘了提到这是在使用MySQL ODBC 5.3驱动程序(libmyodbc5w.so)的Ubuntu 16.04.2 LTS上。


I'm using pymysql (0.7.11) on Python 3.5.2, executing various stored procedures against a MySQL 5.6.10 database. I'm running into a strange and inconsistent issue where I'm occasionally getting zero results returned, though I can immediately re-run the exact same code and get the number of rows I expect.

我在Python 3.5.2上使用pymysql(0.7.11),对MySQL 5.6.10数据库执行各种存储过程。我遇到了一个奇怪且不一致的问题,我偶尔会返回零结果,但我可以立即重新运行完全相同的代码并获得我期望的行数。

The code is pretty straightforward...

代码很简单......

from collections import OrderedDict
import pymysql
from pymysql.cursors import DictCursorMixin, Cursor

class OrderedDictCursor(DictCursorMixin, Cursor):
    dict_type = OrderedDict

try:
    connection = pymysql.connect(   
        host=my_server,
        user=my_user,
        password=my_password,
        db=my_database,
        connect_timeout=60,
        cursorclass=pymysql.cursors.DictCursor
        )

    param1 = '2017-08-23 00:00:00'
    param2 = '2017-08-24 00:00:00'

    proc_args = tuple([param1, param2])

    proc = 'my_proc_name'

    cursor = connection.cursor(OrderedDictCursor)
    cursor.callproc(proc, proc_args)
    result = cursor.fetchall()
except Exception as e:
    print('Error: ', e)
finally:
    if not isinstance(connection, str):
        connection.close()

More often than not, it works just fine. But every once in awhile, it completes almost instantly but with zero rows in the result set. No error that I can see or anything, just nothing... Run it again, and no problem.

通常情况下,它运作得很好。但每隔一段时间,它几乎立即完成,但结果集中的行为零。没有错误,我可以看到或任何东西,只是没有...再次运行,没有问题。

1 个解决方案

#1


1  

Turns out that the problem had nothing to do with pymysql, odbc, etc., but rather was a problem with the order in which the parameters were passed to the stored procedure.

事实证明这个问题与pymysql,odbc等无关,而是与参数传递给存储过程的顺序有关。

On my desktop, I was using Python 3.6 and things worked just fine. I didn't realize, tho, that one of the changes between 3.5.2 and 3.6 affected how items added to a dictionary object via json.loads were ordered.

在我的桌面上,我使用的是Python 3.6并且工作正常。我没有意识到,3.5.2和3.6之间的变化之一影响了通过json.loads添加到字典对象的项目是如何排序的。

The parameters being passed were coming from a dict object originally populated via json.loads... since they were unordered pre-3.6, running the code would occasionally mean that my starttime and endtime parameters were passed to the MySQL stored procedure backwards. Hence, zero rows returned.

传递的参数来自最初通过json.loads填充的dict对象...因为它们在3.6之前是无序的,运行代码有时会意味着我的starttime和endtime参数被向后传递给MySQL存储过程。因此,返回零行。

Once I realized that was the issue, fixing it was just a matter of adding object_pairs_hook=OrderedDict to the json.loads part.

一旦我意识到这是问题,修复它只是将object_pairs_hook = OrderedDict添加到json.loads部分。

#1


1  

Turns out that the problem had nothing to do with pymysql, odbc, etc., but rather was a problem with the order in which the parameters were passed to the stored procedure.

事实证明这个问题与pymysql,odbc等无关,而是与参数传递给存储过程的顺序有关。

On my desktop, I was using Python 3.6 and things worked just fine. I didn't realize, tho, that one of the changes between 3.5.2 and 3.6 affected how items added to a dictionary object via json.loads were ordered.

在我的桌面上,我使用的是Python 3.6并且工作正常。我没有意识到,3.5.2和3.6之间的变化之一影响了通过json.loads添加到字典对象的项目是如何排序的。

The parameters being passed were coming from a dict object originally populated via json.loads... since they were unordered pre-3.6, running the code would occasionally mean that my starttime and endtime parameters were passed to the MySQL stored procedure backwards. Hence, zero rows returned.

传递的参数来自最初通过json.loads填充的dict对象...因为它们在3.6之前是无序的,运行代码有时会意味着我的starttime和endtime参数被向后传递给MySQL存储过程。因此,返回零行。

Once I realized that was the issue, fixing it was just a matter of adding object_pairs_hook=OrderedDict to the json.loads part.

一旦我意识到这是问题,修复它只是将object_pairs_hook = OrderedDict添加到json.loads部分。