[英]MySQL stored procedure sometimes returns 0 rows

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 (

編輯:我現在嘗試了pyodbc以及pymysql,並且具有相同的結果(調用存儲過程時返回零行)。之前忘了提到這是在使用MySQL ODBC 5.3驅動程序(的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

    connection = pymysql.connect(   

    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)
    if not isinstance(connection, str):

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 个解决方案



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.


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.


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部分。



  © 2014-2022 联系我们: