【问题标题】:Parameterized queries using MySQLdb in Python 3在 Python 3 中使用 MySQLdb 的参数化查询
【发布时间】:2015-04-04 18:12:36
【问题描述】:

我正在尝试使用参数化查询通过 Python 脚本将数据插入 MySQL 数据库,而不是将参数格式化为字符串并将应用程序打开以进行 SQL 注入。

这是 Python 代码:

#!/usr/bin/python3

import MySQLdb as mdb

conn = mdb.connect("localhost", "fakeuser", "fakepassword", "testdb")
c = conn.cursor()
c.execute("INSERT INTO test VALUES (%s)", ("Test", ))
conn.commit()
conn.close()

当我运行脚本时,我得到以下堆栈跟踪:

Traceback (most recent call last):
  File "./load.py", line 7, in <module>
    c.execute("INSERT INTO test VALUES (%s)", ("Test", ))
  File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 184, in execute
    self.errorhandler(self, exc, value)
  File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue
  File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 171, in execute
    r = self._query(query)
  File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 330, in _query
    rowcount = self._do_query(q)
  File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 294, in _do_query
    db.query(q)
_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 '%s)' at line 1")

但是,我读过的所有文档都表明 %s 是正确的占位符。我尝试将%s 括在单引号中,但这会导致语句按字面意思插入该字符串,而不是替换给定的值,这是我所期望的。

以下是相关的软件版本:

  • Python 3.2.3
  • MySQL_python-1.2.3-py3.2
  • MySQL 5.5.40-36.1

注意:我使用的是共享主机,无法升级软件。

【问题讨论】:

    标签: python mysql mysql-python


    【解决方案1】:

    MySQL-Python 项目不支持 Python 3 - 请参阅 the project web page,多年来一直表示“即将支持 Python 3”,以及 the cheese shop entry,它仅提供 Python 2.7 版本并表示:

    未来版本将支持 Python-3.0。

    我没有听说过(或者可以通过搜索找到)可用的“-py3.2”发行版;您的主机可能有一些愚蠢的东西(例如this)来让库安装在 Python 3 上,但能够安装库并不能保证它确实可以工作。

    这里是an example of someone running into the same problem on a shared host

    今天我在远程 Web 服务器上切换到 Python v3.2.3 主机鳄鱼。我在 python v2.6.6 上工作的 cgi-script 现在产生 论文错误:

    --> --> 
    Traceback (most recent call last): 
    File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 171, in execute 
    r = self._query(query) 
    File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 330, in _query 
    rowcount = self._do_query(q) 
    File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 294, in _do_query 
    db.query(q) 
    _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 '%s' at line 1") 
    

    %s 语法对库来说是正确的,这是对的;不幸的是,它在 Python 3 中表现不佳。您可以尝试改用命名参数样式,看看是否能让您有所收获,但最好的办法是完全停止使用不受支持的库。例如,如果您的主机还提供an old version of Connector/Python

    【讨论】:

      猜你喜欢
      • 2018-01-02
      • 2018-01-02
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多