My Python environment:
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32

I have a script that has been running for months. It's a cron job and suddenly it started throwing this error:
pymysql.err.Error: (<class 'TypeError'>, TypeError("'int' does not support the buffer interface",))

I don't understand why it had been working fine and just started throwing this error.

Here's the code,
followed by the full error message

import pymysql

conn = pymysql.connect(host='1.2.3.4', port = 1234, user = 'user',  passwd='passwd', db='db')
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 TableX (itemID, sortID)
               VALUES (%s, %s)""",
               [plist[sid], lastQ[0] ] )

cur.close()
conn.close()

The Full output, including error is:

>>> ================================ RESTART ================================
>>> 
plist
12


last[Q]
165


166
Traceback (most recent call last):
  File "C:/...... 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 don't understand what the error actually is from this message

Thanks

cur.execute("""INSERT INTO TableX (itemID, sortID)
VALUES (%s, %s)""",
[plist[sid], lastQ[0] ] )

What type is plist[sid]? If it is a string you now have to cast it to a byte in Python3.x (encode it), possibly the cause is something in Python3.3.

Both plist[sid] and lastQ[0] are ints

I've tried converting with both bytes and bytearray but still getting same error message

Edited 3 Years Ago by chophouse

Fixed now via two different edits.

1. Edited connections.py in pymysql directory per http://code.google.com/p/pymysql/issues/detail?id=55

old code:

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

def unpack_int32(n):
    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)

def unpack_int64(n):
    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)

replaced with this:

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)

2. I added a " % " char between the end of my SQL query and the argument list. Originally it was :

cur.execute("""INSERT INTO TableX (itemID, sortID)
            VALUES (%s, %s)""",
            [plist[sid], lastQ[0] ] )

which I changed to

cur.execute("""INSERT INTO TableX (itemID, sortID)
               VALUES (%s, %s)""" % (plist[sid], lastQ[0])
           )

Now the script is running normally again.

Edited 3 Years Ago by chophouse

This question has already been answered. Start a new discussion instead.