Reputation: 515
I want to run a Dynamic query in python.
I have a combobox with 4 values (Direct, Indirect, Intermediary, Pointed). What I want to do is to write a dynamic query to interrogate my database and return the count of items.
I have this code:
(val
is used to retrieve the value from a combobox)
c=db.cursor()
val=var.get()
query='SELECT count(*) from table where field=' + val
c.execute(query)
Now the query works if I write it like query='SELECT count(*) from table where field="Direct"'
but I want it to work dynamically.
Is there any solution?
Upvotes: 0
Views: 550
Reputation: 25842
try something like this maybe?
db.execute("SELECT count(*) from table where field = %s", [val])
this is assuming you are using pymssql which uses "%s" to bind parameters
Upvotes: 1