pymysql KeyError

Peter Otten __peter__ at web.de
Wed Apr 10 07:20:53 EDT 2013


Chris Green wrote:

> Hello everyone, greetings from Hong Kong.
> 
> I'm a relative noob with Python and have managed to break it. I'm hoping
> someone would be kind enough to help out, or perhaps just point me in the
> right direction.
> 
> I've written a program which pulls in an XML document from an API on a
> server and populates the data into a MySQL table which I will then use to
> create reports. Everything is fine up until the point it tries to write
> the record to MySQL with this statement:
> 
> dbcur.execute("INSERT INTO BackupJobs (bj_bsname, bj_bsid, bj_startdate,
> bj_starttime, bj_enddate, bj_endtime, bj_status, bj_uploadsize) values
> (%s,%s,%s,%s,%s,%s,%s,%s)", newrecord)
> 
> which borks with this:
> 
> Traceback (most recent call last):
> File "./blahblahblah.py", line 99, in <module>
> dbcur.execute("INSERT INTO BackupJobs (bj_bsname, bj_bsid, bj_startdate,
> bj_starttime, bj_enddate, bj_endtime, bj_status, bj_uploadsize) values
> (%s,%s,%s,%s,%s,%s,%s,%s)", newrecord) File
> "/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 100, in
> execute escaped_args = tuple(conn.escape(arg) for arg in args) File
> "/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 100, in
> <genexpr> escaped_args = tuple(conn.escape(arg) for arg in args) File
> "/usr/local/lib/python3.2/dist-packages/pymysql/connections.py", line 650,
> in escape return escape_item(obj, self.charset) File
> "/usr/local/lib/python3.2/dist-packages/pymysql/converters.py", line 31,
> in escape_item encoder = encoders[type(val)] KeyError: <class
> 'builtin_function_or_method'>
> 
> I can dump out the (dummy) contents of the table using:
> 
> for r in dbcur:
> print(r)
> 
> ...so it seems the connection itself is working. User ID has full
> permissions, I'm pretty sure the data types are correct for each field.
> 
> I can provide more code snippets if necessary.
> 
> I'd be very grateful for any help. And if you're in Hong Kong and have
> helped me through this I'll buy you a beer too :-)

Add a

print(newrecord)

before the 

dbcur.execute(...)

line and tell us what it shows. I'm guessing that for one of the values in 
it you forgot to call a method written in C, e. g.

>>> [42].pop
<built-in method pop of list object at 0x7f86f70bc950>
>>> type(_)
<class 'builtin_function_or_method'>

instead of

>>> [42].pop()
42





More information about the Python-list mailing list