Reputation: 343
I have googled and searched this site, but nothing specific appears and cannot get this working. Here is the code:
binData = ''.join(map(lambda x: chr(x % 256), attach.attcoll))
sql_stmt = """INSERT INTO attachments (attno,filename,fileextension,projNo,procNo,wpattachment) \
VALUES ('%s','%s','%s','%s','%s','%s') ON DUPLICATE KEY UPDATE filename='%s',fileextension='%s'""", attach.attno,\
attach.filename,attach.fileextension,attach.projNo,attach.procNo,binData,attach.filename,attach.fileextension
try:
cursor.execute(MySQLdb.escape_string(sql_stmt))
conn.commit()
The attach.attcoll is data that comes in over JSON from an SQLite blob and java code on the client side. I then want to write binData to MySQL with MySQLdb, but keep getting a TypeError, and a return code of 500. Any ideas on how to fix this. I want to store binData into a MySQL blob.
Upvotes: 1
Views: 4438
Reputation: 4040
Bunch of things here:
binData = ''.join(map(lambda x: chr(x % 256), attach.attcoll))
sql_stmt = """INSERT INTO attachments (attno,filename,fileextension,projNo,procNo,wpattachment)
VALUES (%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE filename=%s,fileextension=%s"""
params = (attach.attno,attach.filename,attach.fileextension,attach.projNo,attach.procNo,binData,attach.filename,attach.fileextension)
cursor.execute(sql_stmt, params)
conn.commit()
A summary:
cursor.execute()
means the cursor will escape it for you.'%s'
, MySQLdb does that for you as well.conn.commit()
, it is not necessary if you aren't using a transactional engine.Upvotes: 1