I get an error in the following Python code:
My Mini Project
def dbconnect():
try:
db=mysql.connector.connect(host='localhost',user='SYSTEM', password='123456', database='billing_system')
mycursor=db.cursor()
mycursor.execute('CREATE TABLE IF NOT EXISTS bill( bill_no int NOT NULL PRIMARY KEY, c_name varchar(20) DEFAULT NULL, c_phone varchar(10) DEFAULT NULL, item varchar(20) DEFAULT NULL, rate int DEFAULT NULL, quantity int DEFAULT NULL, total int DEFAULT NULL);')
mycursor.execute('INSERT INTO bill (bill_no, c_name, c_phone, item, rate, quantity, total) VALUES (%i,%s,%s,%s,%i,%i,%i);', (b,a,c,i,r,q,s))
db.commit()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
db.close()
The error says:
384
<class 'int'>
Not all parameters were used in the SQL statement
PY_VAR0
The Code runs without any issue.
But the data I enter does not store in my Table.
Kindly check this code and Solve my DB connection issue.
2
Answers
Try using %s for all parameters:
mycursor.execute(‘INSERT INTO bill (bill_no, c_name, c_phone, item, rate, quantity, total) VALUES (%s,%s,%s,%s,%s,%s,%s);’, (b,a,c,i,r,q,s))
You should use
%s
as the placeholder instead of%i
for all data types when using parameterized queries with MySQL.So your code will be like this-