I want to check if the ID exists already but I get this error:
Not all parameters were used in the SQL statement
Code:
JavaScript
x
8
1
Id = "TEST"
2
sql = """SELECT * FROM musics WHERE Id = %s"""
3
dbc.execute(sql, Id)
4
5
row = cursor.rowcount
6
if row == 0:
7
#NOT EXSIST
8
Advertisement
Answer
The second argument to execute()
should be a sequence of values, one for each placeholder token %s
in the query.
You did pass a sequence, but not in the way you intended. Strings are sequences, so you actually passed a sequence of four values – T, E, S, T
, which is too many values, because the query only has one placeholder token.
Pass the string as a one-element tuple, like so:
JavaScript
1
4
1
args = ("TEST",)
2
sql = """SELECT * FROM musics WHERE Id = %s"""
3
dbc.execute(sql, args)
4