首页 文章

如何使用mysql-client将引号''`插入到mariaDB中?

提问于
浏览
1

我正在使用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 回答

  • 1

    根据Amadan的评论,在bobby-tables(防止SQL注入的网站)中,它建议:

    使用Python DB API,不要这样做:不要这样做:cmd =“update people set name ='%s',其中id ='%s'”%(name,id)
    curs.execute(CMD)
    相反,请执行以下操作:cmd =“update people set name =%s其中id =%s”
    curs.execute(cmd,(name,id))

    所以在我的情况下,只需将执行行修改为:

    cmd = "INSERT INTO `current_table` (`id`, `name`) VALUES (NULL, %s);"
    cur.execute(cmd, ("Lily's dog"))
    

    这可以避免引号导致的错误 .

相关问题