gpt4 book ai didi

python - 无法使用 flask 使数据库在本地主机上工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:32 25 4
gpt4 key购买 nike

<分区>

我现在用的是最基本的代码,但还是不行:

from flask import Flask, render_template
from flaskext.mysql import MySQL

mysql = MySQL()
app = Flask(__name__)

# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'people'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

@app.route('/test')
def test():
conn = mysql.connect()
cursor = conn.cursor()
sql = "SELECT * from tbl_user"
cursor.execute(sql)
results = cursor.fetchall()
cursor.close()
conn.close()
return results

if __name__ == "__main__":
app.run(debug=True)

我尝试通过 Mysql cli 在本地主机上连接到我的数据库,它工作正常。我已经使用 mysql -u root 进入并且它接受它然后我使用 use people 它工作正常然后我可以做选择语句。

这是我得到的错误:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 847, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "C:\Python27\lib\site-packages\werkzeug\test.py", line 871, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'tuple' object is not callable

但是如您所见,我一使用 Flask 就收到此错误。

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