Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impossible to insert data with PyMySQL when I use parameter

I'm currently working on a basic query which insert data depending on the input parameters, and I'm unable to perfom it.

cur.execute("INSERT INTO foo (bar1, bar2) values (?, ?)", (foo1, foo2))

I have this error message:

Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/init.py", line 1426, in call return self.func(*args) File "test.py", line 9, in register cur.execute("INSERT INTO foo (bar1, bar2) values (?,?)", (foo1, foo2)) File "/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 108, in execute query = query % escaped_args TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'

foo1 and foo2 are both string type. I tried with %s, same error.

like image 238
gaetanm Avatar asked Nov 03 '22 22:11

gaetanm


1 Answers

It seems like a bug in cursors.py. As suggested here and here you should replace this line in cursors.py:

query = query % conn.escape(args)

With this:

query = query.decode(charset) % conn.escape(args)

In case it didn't work try this one instead:

query = query.decode(charset) % escaped_args
like image 156
Sam R. Avatar answered Nov 15 '22 04:11

Sam R.