简体   繁体   中英

Parameters error in mysql.connector for python

An error regarding SQL just appeared in my program while I didn't change the code at all, and it was working before. Here is a snippet of code that gives error. To give some context, it is a raspberry pi system that records attendance with help of RFID chips.

...
if cursor.rowcount >= 1:
      lcd.clear()
      lcd.message("Overwrite\nexisting user?")
      overwrite = input("Overwite (Y/N)? ")
      if overwrite[0] == 'Y' or overwrite[0] == 'y':
        lcd.clear()
        lcd.message("Overwriting user.")
        time.sleep(1)
        sql_insert = "UPDATE users SET name, room = %s WHERE rfid_uid=%s"
      else:
        continue;
else:
    sql_insert = "INSERT INTO users (name, room, rfid_uid) VALUES (%s, %s, %s)"
lcd.clear()
        
lcd.message('Enter new name')
new_name = input("Name: ")
lcd.clear()
        
lcd.message('Enter room number')
new_room = input("Room: ")
lcd.clear()

####this line below gives error
cursor.execute(sql_insert, (new_name, new_room, id))

db.commit()
...

Here is an error I get:

Traceback (most recent call last):
  File "/home/pi/checkinsystem/ra.py", line 67, in <module>
    cursor.execute(sql_insert, (new_name, new_room, id))
  File "/usr/local/lib/python3.7/dist-packages/mysql/connector/cursor.py", line 561, in execute
    "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

I've tried some slight syntax changes but given that it was working perfectly before, I'm clueless at this point. Any suggestions on how to get this fixed?

Hi i think i had the same problem with you before. try this

else:
    n = input('Enter new name')
    lcd.clear()
    r = input('Enter room number')
    lcd.clear()
    sql_insert = "INSERT INTO users (name, room, rfid_uid) VALUES ("+n+","+r+","+rfid_uid+")"
    lcd.clear()
    cursor.execute(sql_insert)
    db.commmit()

i really hope this helps or maybe needed little adjustments.

Error in SQL syntax. Should be sql_insert = "UPDATE users SET name = %s, room = %s WHERE rfid_uid=%s" instead of sql_insert = "UPDATE users SET name, room = %s WHERE rfid_uid=%s"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM