配置数据库连接


1、安装PyMySQL、SQLAlchemy、Flask-SQLAlchemy

2、配置数据库连接的格式

数据库类型+驱动://用户名:密码@主机地址:端口/数据库名称?charset=编码

3、config.py

SQL = 'mysql'
DRIVER = 'pymysql'
USERNAME = 'root'
PASSWORD = '123456'
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'cms'
CHARSET = 'utf8'

SQLALCHEMY_DATABASE_URI = f'{SQL}+{DRIVER}://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}?charset={CHARSET}'
SQLALCHEMY_TRACK_MODIFICATIONS = False

4、app.py

from flask import Flask

app = Flask(__name__)
app.config.from_pyfile('config.py')

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

results matching ""

    No results matching ""