When i try to insert some datas with python to MySQL database where the fourth column is a variable , (it depends on the variable "e0") , i get a message error

here is the query syntax : cur.execute('insert into suiviactuelms (modelsa,date,heure,%s) values (%s,%s,%s,%s);',(e0, modelidvar, cur_date, t, quantitevar))

and the error i get is :

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 ''1ere') values ('52645','2013-11-14','14:57:27','10')' at line 1")

Edited by Ismatus3

3 Years
Discussion Span
Last Post by woooee


You can't have a named field as "%s" It has to be the field name so you will have to use if statements or create the 'insert into' as a string containing the field names themselves.

if e0 = =1:
    cur.execute('insert into suiviactuelms (modelsa,date,heure,field1) values (%s,%s,%s,%s);',(modelidvar, cur_date, t, quantitevar))

elif e0 == 2:

Edited by woooee


Hello ,
Thank you woooee , it worked with :

cur.execute('insert into sactuelpm (model,date,heure,%s) values (%%s,%%s,%%s,%%s)'%(e0,), (modelidvar, cur_date, t, quantitevar))

But i still don't understand well this part of python , thank you again .


That is the same as

or create the 'insert into' as a string containing the field names themselves.

SQL_lit  = 'insert into sactuelpm (model,date,heure,%s) values (%%s,%%s,%%s,%%s;)' % (e0)
print SQL_lit
cur.execute(SQL_lit, (fields))

which is perfectly legal. It does not matter if you create the execute string in the cur.execute statement or somewhere else, a string is a string.

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.