I have a python script that I want to extract data to a list from a DB, and then print it out.
Here is the code:
sql = "SELECT * FROM example WHERE column1 == 'string_value' AND column2 = float_value AND column3 == 'string_value'"
# Assemble list.
query = []
for row in c.execute(sql):
query.append(row)
This runs fine. When I debug i see sql is a string and the loop runs fine and assembles the list. When I however try to use variables for the sql query columns values using string formatting like this:
sql = "SELECT * FROM example WHERE column1 = %s AND column2 = %s AND column3 = %s ",(var1_str, var2_float, var3_str)
I get a:
for row in c.execute(sql):
ValueError: operation parameter must be str
and I cannot run the loop to assemble the list. Debugging this I see 'sql' is now a tuple and not a string.Why is that? Is there a better way to do this than what I am using. This is my first venture into SQL and Python so I am new to this.
Thanks you for any suggestions.