Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not all parameters were used in the SQL statement when using python and mysql

hi I am doing the python mysql at this project, I initial the database and try to create the table record, but it seems cannot load data to the table, can anyone here can help me out with this

import mysql.connector
mydb = mysql.connector.connect( host="localhost",user="root",password="asd619248636",database="mydatabase")
mycursor = mydb.cursor()
mycursor.excute=("CREATE TABLE record (temperature FLOAT(20) , humidity FLOAT(20))")
sql = "INSERT INTO record (temperature,humidity) VALUES (%d, %d)"
val = (2.3,4.5)
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")

and the error shows "Not all parameters were used in the SQL statement") mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

like image 465
B.JOE Avatar asked Oct 21 '25 16:10

B.JOE


2 Answers

Changing the following should fix your problem:

sql = "INSERT INTO record (temperature,humidity) VALUES (%s, %s)"
val = ("2.3","4.5") # You can also use (2.3, 4.5)
mycursor.execute(sql,val)

The database API takes strings as arguments, and later converts them to the appropriate datatype. Your code is throwing an error because it isn't expecting %d or %f (int or float) datatypes.

For more info on this you can look here

like image 114
Lord Elrond Avatar answered Oct 23 '25 05:10

Lord Elrond


simply change insert method to

sql = "INSERT INTO record (temperature,humidity) VALUES (%s, %s)"

then it works fine

like image 45
B.JOE Avatar answered Oct 23 '25 06:10

B.JOE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!