"INSERT INTO fruit (name, variety) VALUES (%s, %s)" % ("watermelon", "melon")
Diventa letteralmente
INSERT INTO fruit (name, variety) VALUES (watermelon, melon)
Invece di stringhe, watermelon
e melon
sono colonne. Per risolvere questo problema, inserisci tra virgolette il tuo %s
.
"INSERT INTO fruit (name, variety) VALUES ('%s', '%s')" % (new_fruit, new_fruit_type)
Tuttavia, dovresti eseguirlo come:
cursor.execute("INSERT INTO fruit (name, variety) VALUES (%s, %s)", (new_fruit, new_fruit_type));
Nota che abbiamo eliminato le citazioni attorno a %s
e stanno passando le variabili come secondo argomento a execute
metodo. Execute
impedisce l'iniezione di sql dalle variabili e avvolge le stringhe tra virgolette.
Per ulteriori informazioni, vedere http://mysql-python.sourceforge.net/ MySQLdb.html#alcuni-esempi