My query is as below in python flask
result = app._engine.execute ("select bookingid, DATE_FORMAT(bookingdate, '%Y-%m-%d'), DATE_FORMAT(pickupdate, '%Y-%m-%d'), consigneeid, fromcity from booking where consignerid = '{0}'".format(loginid))
This query works when I run in MySQL Workbench and returns date in format yyyy-mm-dd but when I run that in python flask get the below error
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])TypeError: not enough arguments for format string
It's looking for more arguments though the % in the query is for formatting and not for passing arguments. I did try searching for the reason but couldn't find a good one, what am I doing wrong here ?
When using MySQL Connector for Python you should escape literal %, this is because Python uses the percent sign (%) for string formatting.
try this:
result = app._engine.execute ("select bookingid, DATE_FORMAT(bookingdate, '%%Y-%%m-%%d'), DATE_FORMAT(pickupdate, '%%Y-%%m-%%d'), consigneeid, fromcity from booking where consignerid = '{0}'".format(loginid))