gpt4 book ai didi

python - 如何使用 SQLAlchemy 将远程 mySQL 数据库连接到 Flask 应用程序?

转载 作者:行者123 更新时间:2023-11-29 09:50:37 27 4
gpt4 key购买 nike

我想将在线 MySQL DB 连接到我制作的 Flask 应用程序,这样我就可以摆脱 localhost 的东西了。

我有这个代码

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os
import uuid
from werkzeug.security import generate_password_hash , check_password_hash

app = Flask(__name__)

app.config['SECRET_KEY'] = 'thisissecret'
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['sqlite:////Users/confuapplication/PycharmProjects/untitled/.db']
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://saifaliunity:123123123!@saifaliunity.mysql.pythonanywhere-services.com/saifaliunity$todo';
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']

db = SQLAlchemy(app)

这就是本地主机环境,我已经在线设置了 MySQL 和 python 应用程序,并且想要为 Flask 应用程序设置数据库名称、主机、用户名、密码。但不知道该怎么做。

使用下面的答案后,这是错误日志。

2019-02-26 10:16:54,896: Error running WSGI application
2019-02-26 10:16:54,900: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 10:16:54,900: File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 10:16:54,900: from flask_app import app as application # noqa
2019-02-26 10:16:54,901: ***************************************************
2019-02-26 10:16:54,901: If you're seeing an import error and don't know why,
2019-02-26 10:16:54,901: we have a dedicated help page to help you debug:
2019-02-26 10:16:54,901: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 10:16:54,902: ***************************************************
2019-02-26 12:58:20,238: Error running WSGI application
2019-02-26 12:58:20,278: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 12:58:20,279: File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 12:58:20,279: from flask_app import app as application # noqa
2019-02-26 12:58:20,279: ***************************************************
2019-02-26 12:58:20,280: If you're seeing an import error and don't know why,
2019-02-26 12:58:20,280: we have a dedicated help page to help you debug:
2019-02-26 12:58:20,280: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 12:58:20,280: ***************************************************
2019-02-26 12:58:21,842: Error running WSGI application
2019-02-26 12:58:21,843: ModuleNotFoundError: No module named 'flask_app'
2019-02-26 12:58:21,843: File "/var/www/saifaliunity_pythonanywhere_com_wsgi.py", line 16, in <module>
2019-02-26 12:58:21,843: from flask_app import app as application # noqa
2019-02-26 12:58:21,844: ***************************************************
2019-02-26 12:58:21,844: If you're seeing an import error and don't know why,
2019-02-26 12:58:21,844: we have a dedicated help page to help you debug:
2019-02-26 12:58:21,844: https://help.pythonanywhere.com/pages/DebuggingImportError/
2019-02-26 12:58:21,844: ***************************************************

最佳答案

SQLALCHEMY_DATABASE_URI 应为 mysql://username:password@server/db,替换用户名密码 >、serverdb 以及您的值。

有关更多详细信息,请参阅文档:http://flask-sqlalchemy.pocoo.org/2.3/config/

关于python - 如何使用 SQLAlchemy 将远程 mySQL 数据库连接到 Flask 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54882856/

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