2

该代码昨天运行良好,作为 cron 作业运行。今天突然,它不是,我收到这个错误:

    Traceback (most recent call last):
  File "C:/Users/ac33g1r1/Documents/BD_Scripts/test plist script.py", line 28, in <module>
    [plist[sid], lastQ[0]] )
  File "C:\Python33\pymysql\cursors.py", line 117, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python33\pymysql\connections.py", line 187, in defaulterrorhandler
    raise Error(errorclass, errorvalue)
pymysql.err.Error: (<class 'TypeError'>, TypeError("'int' does not support the buffer interface",))

我已经搜索过,无法弄清楚为什么这突然改变了。Windows Server 2008 上的 Python 版本是 3.3.0。我真的很想让这个 cron 工作再次工作,但不知道究竟是什么原因。

这是代码:

import pymysql

conn = pymysql.connect(host='1.2.3.4', port = 1234, user = 'uname',  passwd='pword', db='db_x')
cur = conn.cursor()

lastQ = [165]
plist = [3327, 2145, 3429, 3442, 2905, 3339, 2628, 1655, 1831, 3202, 2551, 2110]

###Debug statements
print("plist")
print(len(plist))
print ("\n")

print("last[Q]")
print(lastQ[0] )
print ("\n")
lastQ[0] = lastQ[0] + 1
print(lastQ[0] )

# Code that is throwing error

for sid in range(len(plist)):
   lastQ[0] = lastQ[0] + 1
   cur.execute("""INSERT INTO queuelist(itemID, sortID)
               VALUES(%s,%s)""",
               [plist[sid], lastQ[0]] )

cur.close()
conn.close()
4

2 回答 2

1

我在 64 位 Windows 7 上也遇到了类似的问题,这个解决方法帮助了我。只需将 pymysql 的connections.py中的unpack_int24、unpack_int32 和 unpack_int64函数替换为

def unpack_int24(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
            (struct.unpack('B',n[2])[0] << 16)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)

def unpack_int32(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
            (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B', n[3])[0] << 24)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)+(n[3]<<24)

def unpack_int64(n):
    try:
        return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0]<<8) +\
        (struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B',n[3])[0]<<24)+\
        (struct.unpack('B',n[4])[0] << 32) + (struct.unpack('B',n[5])[0]<<40)+\
        (struct.unpack('B',n[6])[0] << 48) + (struct.unpack('B',n[7])[0]<<56)
    except TypeError:
        return n[0]+(n[1]<<8)+(n[2]<<16)+(n[3]<<24) \
              +(n[4]<<32)+(n[5]<<40)+(n[6]<<48)+(n[7]<<56)
于 2014-04-28T08:58:28.887 回答
0

Did you upgrade Python or pymysql?

I see in the pymysql issues list that there are various known issues when used with Python 3 that sound similar. Due to the distinction between byte arrays and strings in Python 3 the unpack_* functions in pymysql/connections.py aren't working correctly. See http://code.google.com/p/pymysql/issues/detail?id=55#c3 for an example issue description and a provided work-around.

于 2013-01-07T17:54:14.867 回答