Hello,
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")

Recommended Answers

All 3 Replies

modelsa,date,heure,%s

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:
    etc.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.