0

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

1
  • due to dbapi only %s is allowed Commented Aug 25, 2019 at 9:00

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.