PyMySQL 错误地将变量识别为元组
PyMySQL Incorrectly Identifying Variables as Tuple
我正在尝试在 Python3 中使用 PyMySQL 进行插入,但我不断收到以下信息:
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
我的代码如下:
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
#Execute the query
print("Category: %s" % category)
print("Category is string:",isinstance(category,str))
print("Category_group: %s" % category_group)
print("Category_group is string:",isinstance(category,str))
self.DB['master']['cursor'].execute(query,(category,category_group,))
self.DB['master']['con'].commit()
当 运行 输出时:
Category: Amusement Park
Category is string: True
Category_group: Parks
Category_group is string: True
Traceback (most recent call last):
File "main.py", line 87, in <module>
m()
File "main.py", line 82, in __call__
self.deal.processdeal(deal=item,store=store)
File "file.py", line 85, in processdeal
btype = self.obtainBusinessType(deal['category'],deal['categoryGroup'])
File "file.py", line 59, in obtainBusinessType
self.DB['master']['cursor'].execute(query,(category,category_group,))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/cursors.py", line 130, in execute
query = query % self._escape_args(args, conn)
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
输出语句表明我正在将字符串作为参数,那么为什么它会抱怨元组?
您的问题是如何构建 query
变量。
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
print query
>>>('INSERT INTO table_name ', '(type,sub) ', 'VALUES ', '(%s,%s)')
query = query % ('hello', 'world')
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
请注意您的 query
如何输出为 tuple
,这会导致错误。
您可以使用 str
:
query = "INSERT INTO table_name (type,sub) VALUES (%s,%s)"
或者您从查询中删除 ,
query = ("INSERT INTO table_name "
"(type,sub) "
"VALUES "
"(%s,%s)")
或者,如 Kindall 所述,您还可以对多行字符串使用三引号
"""INSERT INTO table_name
(type,sub)
VALUES
(%s,%s)"""
我正在尝试在 Python3 中使用 PyMySQL 进行插入,但我不断收到以下信息:
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
我的代码如下:
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
#Execute the query
print("Category: %s" % category)
print("Category is string:",isinstance(category,str))
print("Category_group: %s" % category_group)
print("Category_group is string:",isinstance(category,str))
self.DB['master']['cursor'].execute(query,(category,category_group,))
self.DB['master']['con'].commit()
当 运行 输出时:
Category: Amusement Park
Category is string: True
Category_group: Parks
Category_group is string: True
Traceback (most recent call last):
File "main.py", line 87, in <module>
m()
File "main.py", line 82, in __call__
self.deal.processdeal(deal=item,store=store)
File "file.py", line 85, in processdeal
btype = self.obtainBusinessType(deal['category'],deal['categoryGroup'])
File "file.py", line 59, in obtainBusinessType
self.DB['master']['cursor'].execute(query,(category,category_group,))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/cursors.py", line 130, in execute
query = query % self._escape_args(args, conn)
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
输出语句表明我正在将字符串作为参数,那么为什么它会抱怨元组?
您的问题是如何构建 query
变量。
query = ("INSERT INTO table_name ",
"(type,sub) ",
"VALUES ",
"(%s,%s)")
print query
>>>('INSERT INTO table_name ', '(type,sub) ', 'VALUES ', '(%s,%s)')
query = query % ('hello', 'world')
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'
请注意您的 query
如何输出为 tuple
,这会导致错误。
您可以使用 str
:
query = "INSERT INTO table_name (type,sub) VALUES (%s,%s)"
或者您从查询中删除 ,
query = ("INSERT INTO table_name "
"(type,sub) "
"VALUES "
"(%s,%s)")
或者,如 Kindall 所述,您还可以对多行字符串使用三引号
"""INSERT INTO table_name
(type,sub)
VALUES
(%s,%s)"""