Escaping chars in Python and sqlite

前端 未结 4 2067
温柔的废话
温柔的废话 2020-12-05 06:21

I have a python script that reads raw movie text files into an sqlite database.

I use re.escape(title) to add escape chars into the strings to make them db safe befo

4条回答
  •  青春惊慌失措
    2020-12-05 06:43

    You're doing it wrong. Literally. You should be using parameters, like this:

    c.execute("UPDATE movies SET rating = ? WHERE name = ?", (8.7, "'Allo 'Allo! (1982)"))
    

    Like that, you won't need to do any quoting at all and (if those values are coming from anyone untrusted) you'll be 100% safe (here) from SQL injection attacks too.

提交回复
热议问题