Search code examples
pythonbetter-sqlite3

Query regarding placeholder in SELECT statement in sqlit3 library


I created a table and added records to it but I want to print each record as I add more records.But the code below doesn't seem to be working for me.. Btw I input model from user..

Cursor.execute("SELECT * FROM Top_Cars WHERE MODEL=?;",(model))
print(Cursor.fetchone())

Solution

  • It is a common mistake with tuples. (model,) is a 1-tuple, but (model) is just a parenthesed expression containing only model. As a string is an iterable of characters, execute sees as many bindings as letters in model word. Fix it trivial:

    Cursor.execute("SELECT * FROM Top_Cars WHERE MODEL=?;",(model,))
    

    or:

    Cursor.execute("SELECT * FROM Top_Cars WHERE MODEL=?;",[model])