Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to update some data into a mysql table, there are no warnings/errors produced but the data inserted has an extra character 0 and the string is truncated. This is the code from Python shell:

>>> len(d) #d is a string with contents of an html email
12347
>>> d[:100]
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.=\r\nw3.org/TR/html4/loose.'
>>> a = mysql.connector.connect(user='root', password='mypass', database='mydb')
>>> cur = a.cursor(prepared=True)
>>> cur.execute('UPDATE emails set content=? where emailid=10', (d,))
>>> a.commit()
>>> cur.execute('SELECT content from emails WHERE emailid=10')
>>> cur.fetchall()
[('0<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional/',)]

As you can see the content is truncated and also has an extra '0' . The type of 'content' field is MEDIUMTEXT, I tried with and without sql_mode as strict, but the result is same.

share|improve this question
1  
Try running the query without prepared=True, this is a known bug in mysql-connector-python bugs.mysql.com/bug.php?id=73690 which will be fixed in upcoming versions. –  Peeyush Sep 26 '14 at 12:47
    
@Peeyush Thanks for confirming this. I suspected this was the case but couldn't find out the relevant bug in the tracker. I hadn't tried omitting prepared=True . I assumed I could run prepared queries only with that option which isn't the case. Thanks again, I was about to change my code to use MySQLdb and lose connection pooling. –  Ravi Sep 26 '14 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.