Status Quo:
I have a working database with tables and can query, insert, update, etc. Also the cursor is connected to the right database.
The table:
Problem:
When it comes to querying data from a table I run into trouble:
query = 'SELECT Last_Request_Time FROM Products WHERE idProduct = %s'
idProduct = '106'
cursor.execute(query, (idProduct))
While debugging I have a look at the cursor.execute() function: params = str: 106
will be passed to:
stmt = operation % self._process_params(params)
where
res = params
# pylint: disable=W0141
res = map(self._connection.converter.to_mysql, res)
is called with res = str: 106
. I am not sure what the converter is doing but as result res = list: ['1', '0', '6']
. And these arguments will be passed to the execute function which will run into following error:
File "C:Python27libsite-packagesmysqlconnectorcursor.py", line 480, in execute
"Wrong number of arguments during string formatting")
mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting
Bad workaround:
I have a dirty workaround, but I am not happy with it. It may not work in some cases:
query = 'SELECT Last_Request_Time FROM Products WHERE idProduct = %s AND Edition != %s'
idProduct = '106'
cursor.execute(query, (idProduct, 'A'))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…