0

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.

James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

0

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))

  • I have intitialized the following variables as IntVar (To help my calculation part) I have attached my full program's GitHub link too. – Krishnakumar R Apr 28 '23 at 18:04
  • check out this response, I believe the %s is not the same as a string formatting. https://stackoverflow.com/a/20818201/20602568 – ryan zalewski Apr 28 '23 at 18:07
  • So are you trying to say me that i should change to %s and if it does not work, I should change the same to just '? ' ? @ryan zalewski – Krishnakumar R Apr 29 '23 at 01:24
0

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-

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 (%s,%s,%s,%s,%s,%s,%s);', (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()
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

I've resolved my issue.. The key was to initialize the variables just before inserting the data in DB... Thank you all for these wonderful suggestions..