1

I'm trying to use Python to do an SQLite query. I can get the desired result in SQLite using

SELECT Four FROM keys2 WHERE One = B

How do I do this in Python?

I am trying

c = conn.cursor()

print "Opened database successfully";
x = c.execute("SELECT Four FROM keys2 WHERE One = B")
print x

conn.close()

But I get the message "sqlite3.OperationalError: no such column: B". I'm trying to select the entry under column 4 where the one column is B. I have tried several methods to no avail.

1 Answer 1

2

Supply the value using parametrized SQL:

x = c.execute("SELECT Four FROM keys2 WHERE One = ?", "B")

The problem with the SQL you posted is that the value B must be quoted. So

x = c.execute("SELECT Four FROM keys2 WHERE One = 'B'")

would also have worked, but it is better to always use parametrized SQL (to guard against SQL injection) and let sqlite3 do the quoting for you.

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

2 Comments

When I run that I get the following <sqlite3.Cursor object at 0x107afeab0>. Do I need to convert this type? It is a text type in sqlite.
Added print x.fetchall() and looks like it is working. Thanks

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.