gpt4 book ai didi

python - 即使我在调用 fetchall 之前执行,MySQLdb 也会引发 "execute() first"错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:26 26 4
gpt4 key购买 nike

我在尝试将 MySQLdb 与 Flask 结合使用时遇到以下错误。 _mysql_exceptions.ProgrammingError: 首先执行()

print 输出显示“After fetch”永远不会被打印出来,这意味着脚本在 row = g.db.cursor().fetchall()。为什么会出现此错误以及如何解决?

def connect_db():
return mysql.connection

@app.before_request
def before_request():
g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
g.db.cursor().close()

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
g.db.cursor().execute('SELECT * FROM users WHERE email = %s', [request.form['email']])
print('Before fetch')
row = g.db.cursor().fetchall()
print('After fetch')

if not row:
flash('No such user exists!')
return redirect(url_for('show_home'))
elif request.form['password'] == row[0][1]:
return redirect(url_for('show_dashboard'))
else:
flash('Wrong password / login error.')
return redirect(url_for('show_home'))
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/media/common/code/python/projects/up2date/up2date.py", line 47, in login
row = g.db.cursor().fetchall()
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 406, in fetchall
self._check_executed()
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 107, in _check_executed
self.errorhandler(self, ProgrammingError, "execute() first")
File "/media/common/code/python/projects/up2date/venv/lib/python3.4/site-packages/MySQLdb/connections.py", line 38, in defaulterrorhandler
Open an interactive python shell in this frameraise errorclass(errorvalue)

最佳答案

您多次调用 cursor,这会创建多个游标。您在第一个游标上执行查询,然后从第二个游标获取。由于您没有在第二个执行任何操作,因此会出现此错误。只使用一个光标。

cursor = g.db.cursor()
cursor.execute(...)
rows = cursor.fetchall()

关于python - 即使我在调用 fetchall 之前执行,MySQLdb 也会引发 "execute() first"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com