MySQL存儲過程有時會返回0行

[英]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 (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部分。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/08/25/72099793b5d0fa84dd723057c240c905.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com