Python将html插入MySQL

标签 python mysql syntax-error

我正在尝试将 HTML 代码插入 MySQL 数据库的字段中。

我使用下面的代码来做到这一点

cur.execute("INSERT INTO `table1`(field1) VALUES("+ str(data) +")")

但我收到以下错误:

_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right 
syntax to use near \'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.or\' at line 3')

如何解决这个问题?提前致谢!

最佳答案

你应该转义你的字符串:

cur.execute("INSERT INTO `table1`(field1) VALUES('"+ conn.escape_string(str(data))+"')")

其中 conn 是您的连接。

关于Python将html插入MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11972470/

相关文章:

python - 分割字符串时丢失编码

python - 如何在python中将一个csv拆分为多个文件

Java SQL 插入多个变量

php - CodeIgniter 计数太慢 - 分页

Javascript 函数未定义且语法错误?

python - 使用 session 的Django View 中 “def”的语法错误

python - 用 pandas 和 matplotlib_venn 绘制维恩图

python - Django View : Count specific value from model

sql - 选择查询问题

php - 我想通过 retrofit2 api 从 Mysql 数据库中获取数据