krishna Gundewar
krishna Gundewar

Reputation: 3

Delete user input from MySQL

I'm takin input from user and trying to delete that data MySQL but my code doesn't work. I can't find error on my code. Appriciate aditional pair of eyes.

import mysql.connector


    Accno = int(input("Enter account number which you delete"))

    mydb = mysql.connector.connect(host="localhost", user="root", passwd="root",database="abc")
    mycursor = mydb.cursor()

    sql = "Delete from addacc1 where accno = %s"
    val = (Accno)
    mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record Deleted.")

After executing script I'm getting this error.

Traceback (most recent call last):
  File "C:/Users/K/PycharmProjects/Pratice/DELETE record in db.py", line 13, in <module>
    mycursor.execute(sql, val)
  File "C:\Users\K\PycharmProjects\Pratice\venv\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\K\PycharmProjects\Pratice\venv\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Users\K\PycharmProjects\Pratice\venv\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

Upvotes: 0

Views: 1072

Answers (1)

Anton
Anton

Reputation: 437

You need to change

val = (Accno)

into

val = (Accno,)

for the reference from the official docs:

a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective

Upvotes: 1

Related Questions