Sarra Ouertani
Sarra Ouertani

Reputation: 1

Fix %s sql query in python

Python refuses to execute an sql query

I want to execute an insert sql query with python language in Pycharm. An error is launched after executing the code below:

import mysql.connector

stock = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="admin2020",
  database="stock"
)

sql = "INSERT INTO produit(code, nom, prix_unitaire, tva, quantite) VALUES(%s,%s,%f,%f,%d)"
valeurs = ("LAM","lampe",0.9,0.19,10)
mycursor = stock.cursor()
mycursor.execute(sql, valeurs)

The error message is:

mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

Upvotes: 0

Views: 781

Answers (2)

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

You're confusing %s used as a parameter marker for a query, and %s (%d, %f, etc) used in Python string formatting. In your queries you should use just %s, the database driver will format the query for you:

sql = ("INSERT INTO produit(code, nom, prix_unitaire, tva, quantite) "
       "VALUES (%s, %s, %s, %s, %s)")
valeurs = ("LAM", "lampe", 0.9, 0.19, 10)
mycursor.execute(sql, valeurs)

Upvotes: 1

Primit
Primit

Reputation: 865

Try this one

sql = "INSERT INTO produit(code, nom, prix_unitaire, tva, quantite) VALUES(%s,%s,%f,%f,%d)"%("LAM","lampe",0.9,0.19,10)

    mycursor = stock.cursor()
    mycursor.execute(sql)

Upvotes: 0

Related Questions