我正在使用mariaDB(Ver 15.1 Distrib 10.0.17-MariaDB,for osx10.10(x86_64))和 mysqlclient==1.3.6
.
我只想在varcharfield中插入一个字符串 .
import MySQLdb
import json
conn = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='',
db='ng')
cur = conn.cursor()
cur.execute(INSERT INTO `current_table` (`id`, `name`) VALUES (NULL, '{name}');".format(name="Lily' dog"))
conn.commit()
但我总是遇到这样的错误:
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 's dog', NULL)' at line 1")
如果我想通过mysql-client插入引号,我该怎么办?
1 回答
根据Amadan的评论,在bobby-tables(防止SQL注入的网站)中,它建议:
所以在我的情况下,只需将执行行修改为:
这可以避免引号导致的错误 .