这个查询很简单,但是当文本包含一些引号时它就不起作用了。
cursor.execute ("INSERT INTO text (text_key, language_id, text) VALUES ('%s', '%s', '%s')" % (key, language_id, text))
保护文本变量的最佳方法是什么?
最佳答案
始终将参数与查询分开传递:
cursor.execute (
"INSERT INTO text (text_key, language_id, text) VALUES (%s, %s, %s)",
(key, language_id, text))
这样才能正确处理引用。
关于python - 使用 MySQLdb 执行 INSERT 时如何保护文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3311417/