python - Cannot remove SQLite row using 'where' and variable -
got problem, appreciate on it.
i want remove row, transferred func variable. that's code:
con = sql.connect('db.sqlite') cur = con.cursor() query = 'delete media_tmp media_id="%s"' % media_id.strip() print(query) cur.execute(query) if con: con.close()
print(query) gives me following:
delete media_tmp media_id="737589711821419460_184456611"
and when execute directly in sqlite, works , removes deserved row. executed cur.execute(query) doesn't work @ all.
the func finishes good, got no errors.
you forgetting commit change:
con.commit()
the sqlite3 command line tool auto-commits changes, sqlite3
library not.
you should use sql parameters instead of string interpolation:
query = 'delete media_tmp media_id=?' cur.execute(query, (media_id.strip(),))
quoting sqlite3
documentation:
usually sql operations need use values python variables. shouldn’t assemble query using python’s string operations because doing insecure; makes program vulnerable sql injection attack (see http://xkcd.com/327/ humorous example of can go wrong).
instead, use db-api’s parameter substitution. put
?
placeholder wherever want use value, , provide tuple of values second argument cursor’sexecute()
method. (other database modules may use different placeholder, such%s
or:1
.)
Comments
Post a Comment