File "E:\python3.7.7\lib\site-packages\flask_sqlalchemy\extension.py", line 308, in init_app "Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set." RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
时间: 2024-03-22 13:42:15 浏览: 188
python-3.7.7-docs-html_Python-3.7.7_python教程_
5星 · 资源好评率100%
This error message indicates that the Flask-SQLAlchemy extension was not able to find the configuration variable `SQLALCHEMY_DATABASE_URI` or `SQLALCHEMY_BINDS` in your Flask application's configuration.
To fix this error, you need to make sure that you have set the `SQLALCHEMY_DATABASE_URI` or `SQLALCHEMY_BINDS` configuration variable in your Flask application's configuration.
Here is an example of how to set the `SQLALCHEMY_DATABASE_URI` configuration variable:
```
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/mydatabase'
```
In this example, `username` and `password` should be replaced with your MySQL database username and password, `localhost` should be replaced with the hostname of your MySQL server, and `mydatabase` should be replaced with the name of your MySQL database.
If you need to connect to multiple databases, you can set the `SQLALCHEMY_BINDS` configuration variable instead:
```
app.config['SQLALCHEMY_BINDS'] = {
'db1': 'mysql://username1:password1@localhost/db1',
'db2': 'mysql://username2:password2@localhost/db2'
}
```
In this example, `db1` and `db2` are the names of the databases that you want to connect to, and `username1`, `password1`, `username2`, and `password2` should be replaced with the appropriate credentials for each database.
阅读全文