Python,mysql.connector错误:没有结果可取自; cgitb显示正确的值传递给函数 [英] Python, mysql.connector Error: No Result Set to Fetch From; cgitb shows correct value being passed to function
问题描述
好吧,这让我感到困惑,所以我决定看看我是否可以在这里找到答案,我上下搜索了几个stackoverflow问题和答案,但似乎没有任何效果.我想要做的只是使用mysql.connector的SELECT * FROM语句,并且不断收到无结果集"错误.这是代码:
Allright this one's got me baffled so I decided to see if I could find a response on here, I've searched up and down and several stackoverflow questions and answers and nothing has seemed to work. All I'm trying to do is a SELECT * FROM statement using mysql.connector and I keep getting a "No Result Set" error. Here's the code:
def session_fetch(value1):
cnx = mysql.connector.connect(user='xxx', password='xxx',
host='127.0.0.1', database='xxx')
cursor = cnx.cursor()
query = ("SELECT * FROM sessionkeys "
"WHERE clientName='%s';") % value1
cursor.execute(query)
row = cursor.fetchall()
results = len(cursor.fetchall())
clientName, clientAddr, unLocker = row[1], row[2], row[3]
cnx.commit()
cursor.close()
cnx.close()
来自cgitb的错误显示:
session_fetch中的C:\ inetpub \ wwwroot \ flighttoolsbz \ validator.py(value1 ='ericdsmith86')
162 cursor.execute(query)
163行= cursor.fetchall()
=> 164个结果= len(cursor.fetchall())
165 clientName,clientAddr,unLocker =第[1]行,第[2]行,第[3]行
166 cnx.commit()
The error from cgitb shows:
C:\inetpub\wwwroot\flighttoolsbz\validator.py in session_fetch(value1='ericdsmith86')
162 cursor.execute(query)
163 row = cursor.fetchall()
=> 164 results = len(cursor.fetchall())
165 clientName, clientAddr, unLocker = row[1], row[2], row[3]
166 cnx.commit()
InterfaceError:未设置要提取的结果.
args =(-1,'未设置要获取的结果.',无)
errno = -1
msg ='未设置要获取的结果.'
sqlstate =无
with_traceback =接口错误对象的内置方法with_traceback
InterfaceError: No result set to fetch from.
args = (-1, 'No result set to fetch from.', None)
errno = -1
msg = 'No result set to fetch from.'
sqlstate = None
with_traceback = built-in method with_traceback of InterfaceError object
但是,当我浏览MySQL工作台并使用相同的输入值运行相同的查询时,它返回我要查找的一行,因此它肯定在那里.我唯一能想到的是%s格式化程序没有将传递给函数的内容作为'value1'.我想念什么?
But when I go through the MySQL workbench and run the same query using the same input value, it returns the one row i'm looking for, so it's definitely there. The only thing I can think of is that the %s formatter isn't taking what's being passed to the function as 'value1'. What am I missing?
推荐答案
您两次调用cursor.fetchall().你不应该那样做.
You're calling cursor.fetchall() twice. You shouldn't be doing that.
更改:
row = cursor.fetchall()
results = len(cursor.fetchall())
clientName, clientAddr, unLocker = row[1], row[2], row[3]
收件人:
rows = cursor.fetchall()
results = len(rows)
if results > 0:
row = rows[0]
clientName, clientAddr, unLocker = row[1], row[2], row[3]
尽管它与您当前的问题没有任何关系,但您应该使用参数化查询:
And while it doesn't have anything to do with your current problem, you should be using a parameterized query:
query = "SELECT * FROM sessionkeys WHERE clientName=?"
cursor.execute(query, (value1,))
这篇关于Python,mysql.connector错误:没有结果可取自; cgitb显示正确的值传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!