5

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.

gaetanm
  • 1,394
  • 1
  • 13
  • 25

1 Answers1

2

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
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • I didn't find this line `query = query % conn.escape(args)` in my cursors.py, so I tried to modify this line `query = query % escaped_args` (related to this [post](https://github.com/petehunt/PyMySQL/issues/83)) by your line. But now I have this: "TypeError: not all arguments converted during string formatting" – gaetanm May 01 '13 at 20:49
  • 1
    Thanks a lot! It works with `query = query.decode(charset) % escaped_args` and by using `%s` instead of `?`. :D – gaetanm May 01 '13 at 21:02