shubhamprashar
shubhamprashar

Reputation: 54

Searching SQLite3 database with WHERE clause

My code:

   name1 = name_entry.get()
    conn = sqlite3.connect('test123.db')
    c = conn.cursor()
    c.execute("SELECT * FROM table123 WHERE name = name1")
    print(c.fetchall())
    conn.commit()
    conn.close()

Error:

sqlite3.OperationalError: no such column: name1

My question How do I use a variable with WHERE caluse to find the desired tuples ?

Upvotes: 0

Views: 29

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

You should substitute the variable name1 value in your c.execute statement. Do this:

c.execute("SELECT * FROM table123 WHERE name = {}".format(name1))

OR:

c.execute("SELECT * FROM table123 WHERE name = ?", name1)

Upvotes: 1

Related Questions