使用下面的查询时出现以下错误,我在执行cursor.fetchall时已经给出了变量,不确定 为什么我得到这个错误,如何克服这个?
查询: -
query = """SELECT metabuild,testbed FROM gerrits.pw WHERE warehouse ='%s'"""%(warehouse_name)
rows = cursor.execute(query)
(metaBuild,testbed)= cursor.fetchall()
错误: -
(metaBuild,testbed)= cursor.fetchall()
ValueError: need more than 1 value to unpack
答案 0 :(得分:2)
fetchall
返回一个元组列表,每行一个元组。如果您确定查询只返回一行,请执行
(metaBuild,testbed)= cursor.fetchall()[0]
答案 1 :(得分:1)
或者,使用fetchone()
:
metaBuild, testbed = cursor.fetchone()
此外,不要通过字符串格式化或插值来进行查询 - 这样您就会使代码容易受到SQL注入攻击。相反,"参数化"查询:
query = """
SELECT
metabuild, testbed
FROM
gerrits.pw
WHERE
warehouse = %s
"""
cursor.execute(query, (warehouse_name, ))