我正在尝试将 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/