在pymysql查询中的Qmark

时间:2014-06-17 10:36:17

标签: python python-3.x pymysql

我是python的新手,只是从php5移动。我读了关于准备查询的qmark param符号,但我得到以下内容:

Traceback (most recent call last):
  File "t.py", line 10, in <module>
    cursor.execute("SELECT * FROM object WHERE otype = ?", ["user"])
  File "/usr/local/lib/python3.4/site-packages/pymysql/cursors.py", line 130, in execute
    query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting

仅适用于sqlite连接器吗?

import sys
import pymysql

try:
    connection = pymysql.connect(host="127.0.0.1", user="root", passwd="pass", db="xxx")
except:
    sys.exit("Database connection error")

cursor = connection.cursor()
cursor.execute("SELECT * FROM object WHERE otype = ?", "user")

for row in cursor:
   print(row)

cursor.close()
connection.close()

1 个答案:

答案 0 :(得分:1)

是。不幸的是,DB-API规范让每个实现者都可以选择参数。 pysqlite选择了?,而MySQLdb选择了%s。由于pymsql是MySQLdb的替代品,因此它也使用%s。