Search code examples
pythonsqlitesql-delete

Why is there a comma after the variable in this sqlite query?


I have the following code and it works, but I need to understand why there is a comma after the variable id_to_remove, can someone explain to me why that has to be there? (I bolded the part I don't understand)

def delete_user(id_to_remove):
    sql = "DELETE FROM USERS WHERE ID = ?"
    conn = create_connection()
    cursor = conn.cursor()
    **cursor.execute(sql, (id_to_remove, ))**
    conn.commit()
    conn.close()

So I don't know why its id_to_remove, and not just cursor.execute(sql,(id_to_remove))


Solution

  • Because it will replace all the ? values with the ones in the tuple. If you don't put a comma then it is a single value, you need to add it to construct a single value tuple.