I am trying to pass some values from a list into an insert statement and thought it would be straightforward enough but I find myself wasting far too much time on this:
please could someone explain what's going wrong here?
WORKS WITH HARDCODED LIST of values
cursor=db.cursor()
cursor.execute(" INSERT INTO a (a,b) VALUES (%s,%s)", ('abc', 'abc'))
db.commit()
cursor.close()
DOESN'T WORK with list of values as a variable (output_insert)
output = ('abc','abc')
output_insert = str(output)
cursor=db.cursor()
sql = '" INSERT INTO a (a,b) VALUES (%s,%s)",' + output_insert
cursor.execute(sql)
db.commit()
cursor.close()
if I print the sencond statement, I can see that it is identical to the first that fails:
print sql
" INSERT INTO a (a,b) VALUES (%s,%s)",('abc', 'abc')
same as:
" INSERT INTO a (a,b) VALUES (%s,%s)", ('abc', 'abc')
The error returned is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'" INSERT INTO a (a,b) VALUES (%s,%s)",(\'abc\', \'abc\')\' at line 1')
Thanks!