2

I'm looking for the way to be able to compare what is in a Python variable with something in a SQLite query.

For example:

num = input ("enter a number")
cur.execute("SELECT a.Name,a.Number FROM Ads a WHERE a.Number = (num)")
row = cur.fetchone()
while row:
    print(row) 
    row = cur.fetchone()

The cur.execute line doesn't work. I don't know how to be able to compare content of a Python variable with data in a SQLite database via a SQlite query.

1 Answer 1

5

You put a '?' as a place holder for each Python variable, something like

num = 5
cur.execute("SELECT Name, Number FROM Ads WHERE Number = ?", (num,))

The execute() method expects a sequence as a second argument, since several '?' positional placeholders can be used in the SQL statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.