我正在尝试使用PythonAnywhere将一个小型flask应用程序联机。我先将SqlAlchemy与MySql一起使用,但是每当我尝试通过我的应用程序查询内容时,标题中都会出现typeError。然后我转而使用Sqlite3,以为它可以解决问题,但仍然存在。
在尝试将其移植到PythonAnywhere之前,我使用了Sqlite并将其托管在自己的计算机上。我首先使用SqlAlchemy和MySql,因为它是推荐的。我的查询没有问题,因为当我通过控制台执行查询时,它们始终有效。
这是我的代码:
def register():
if request.method == "POST":
[unnecessary code snipped out]
# This is a dummy query, I cut the original out because it was too
# long and unnecessary, but both produce the same error
result = db.execute("""select * from users""")
if not result:
return apology("Username already exists.")
# remember which user has logged in
session["user_id"] = result
# redirect user to home page
return redirect(url_for("index"))
elif request.method == "GET":
return render_template("register.html")
如果用户成功注册,该函数应该将用户重定向到索引,并且查询应该可以工作,但我却收到此错误和回溯:
Traceback (most recent call last):
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1835, in full_dispatch_request
return self.finalize_request(rv)
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 1852, in finalize_request
response = self.process_response(response)
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask/app.py", line 2133, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/flask_session/sessions.py", line 355, in save_session
total_seconds(app.permanent_session_lifetime))
File "/home/8BitRebellion/.virtualenvs/my-virtualenv/lib/python3.6/site-packages/werkzeug/contrib/cache.py", line 839, in set
pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
TypeError: can't pickle sqlite3.Cursor objects
我不明白为什么会触发此错误,并且当我进行搜索时也会遇到类似的错误,但是没有一个与此相关。 预先感谢
编辑:已解决 多亏@roganjosh,我才意识到问题出在我执行查询的方式上,而不是我最初想到的那样处理查询的方式。