I have a function like this:
def func(self, id):
self.cursor.execute("SELECT * FROM my_table WHERE id=?", (id,))
This works when I pass an integer value in id:
obj.func(id=55)
Now I might want to reuse this function like so:
obj.func(id="associated_id")
Where associated_id is actually a second column in my_table. However this doesn't work (it finds no results even though there is a row where id==associated_id).
It works if I change the function like so:
def func(self, id):
self.cursor.execute("SELECT * FROM my_table WHERE id=%s" % str(id))
Why does the first version not work? Is there any way to solve this problem using sqlite3.execute parameters instead of native python string formatting?