How to count items based on 2 arguments?

Question:

My table :

id rack rack_loc ent_date product serial state status
1 RACK 7 A 05/05/2022 ENT5000X 8675762 READY READY

I want to return the COUNT of products with status READY. I have a set with the product called prod (which contains same text as the product column). Below code results in an error:

cursor.execute('''SELECT COUNT(*) FROM test_data WHERE product = prod AND status = 'READY';''')

The error :

no such column: prod

What I expect was a returned value of 1, or more if table was full.

Asked By: PyMan85

||

Answers:

Just use placeholders (?) and query parameters for the dynamic data.

prod = "ENT5000X"  # or whatever
cursor.execute(
    "SELECT COUNT(*) FROM test_data WHERE product = ? AND status = ?",
    [prod, "READY"],
)
count = cursor.fetchone()[0]  # `count` is 0 or more
Answered By: AKX