Stec
Stec

Reputation: 37

sqlite3 python count() query error no such column

I'm trying to produce a query on the number of times the product Americano comes up in my database table. My database looks like this:

id  product size    milkOptions

1   Americano   Small   WholeMilk

2   Espresso    Large   SemiSkimmed

This is the query:

conn=sqlite3.connect("system.db")
cur=conn.cursor()
americano = cur.execute("""SELECT COUNT(id) FROM customerOrders WHERE product = Americano""")

The error is:

americano = cur.execute("""SELECT COUNT(id) FROM customerOrders WHERE product = Americano""")
sqlite3.OperationalError: no such column: Americano

Upvotes: 0

Views: 355

Answers (1)

You are checking if product's value is Americano so you need say Americano is a string literal, not just a variable.

Currently you are telling SQL to grab all rows where the product column is equal to the Americano column.

The following should fix that issue:

conn=sqlite3.connect("system.db")
cur=conn.cursor()
americano = cur.execute("""SELECT COUNT(id) FROM customerOrders WHERE product = 'Americano'""")

Upvotes: 1

Related Questions