Thom Rogers
Thom Rogers

Reputation: 1433

Python error when querying MySQL -- 'int' does not support the buffer interface

This code was working fine yesterday, running as a cron job. Suddenly today, it is not and I am getting this error:

    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",))

I've searched and can't figure out why this has suddenly changed. Python version is 3.3.0 on Windows Server 2008. I'd really tlike to get this cron job working again, but don't know what is actually the cause.

Here is the code:

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()

Upvotes: 2

Views: 2514

Answers (2)

Mike
Mike

Reputation: 2153

I also had the similar problem on 64-bit Windows 7 and this workaround helped me. Just replace unpack_int24, unpack_int32 and unpack_int64 functions in pymysql's connections.py with

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)

Upvotes: 1

Simeon
Simeon

Reputation: 441

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.

Upvotes: 0

Related Questions