python - 使用 MySQLdb 执行 INSERT 时如何保护文本

标签 python mysql

这个查询很简单,但是当文本包含一些引号时它就不起作用了。

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/

相关文章:

python - 狮身人面像 : customize sidebar section titles

mysql - 为什么 BETWEEN 在此 MySQL 查询中不起作用?

MySQL 服务器不通过 phpmyAdmin 创建存储过程

php - 当我在 phpmyadmin 的 config.inc.php 中设置 hide_db 时,所有数据库都消失了

python - BeautifulSoup 如何在 <br> 标签后提取文本

python pygame 背景图像随屏幕大小变化

python - 在二维数组上使用插值函数

python , Selenium : unable to get raw html from javascript

php - 显示数据库 php 中的 2 张图片

mysql - 从具有最大/最小值的记录中提取值