pymysql - TypeError: not all arguments converted during string formatting with sql execute in Python -
the code below
varlist =[] key, value in json_obj.iteritems(): print key, value varlist = varlist + ["string"] print varlist params = ['?' item in varlist] query_string = 'insert test_table values (%s);' % ','.join(params) print query_string cursor.execute(query_string, varlist)
produces following
['string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'] insert test_table values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?); traceback (most recent call last): file "./vpn.py", line 26, in <module> cursor.execute(query_string, varlist) file "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 164, in execute query = self.mogrify(query, args) file "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 143, in mogrify query = query % self._escape_args(args, conn) typeerror: not arguments converted during string formatting
i have tried
cursor.execute(query_string, (varlist,))
but doesn't work either.any idea problem is?
from example in documentation:
insert_stmt = ( "insert employees (emp_no, first_name, last_name, hire_date) " "values (%s, %s, %s, %s)" ) data = (2, 'jane', 'doe', datetime.date(2012, 3, 23)) cursor.execute(insert_stmt, data)
note %s
in query line. that's how cursor.execute
knows put values. in case query filled , results in error.
Comments
Post a Comment