sqlite - Python sqlite3 string formatting -


i have function this:

def func(self, id):     self.cursor.execute("select * my_table id=?", (id,)) 

this works when pass integer value in id:

obj.func(id=55) 

now might want reuse function so:

obj.func(id="associated_id") 

where associated_id second column in my_table. doesn't work (it finds no results though there row id==associated_id).

it works if change function so:

def func(self, id):     self.cursor.execute("select * my_table id=%s" % str(id)) 

why first version not work? there way solve problem using sqlite3.execute parameters instead of native python string formatting?

in first case doing parameter binding. correct, sqlite libraries bind value 55 query call. in second case should not use parameter binding because sql equivalent to:

select * my_table id='associated_id' 

because binding call detects passing string , treats internally string.

when

"select * my_table id=%s" % str(id) 

you don't parameter binding pass sql sqlite.

some documentation on parameter binding in:

http://www.sqlite.org/c3ref/bind_blob.html


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -