python3 + flask + sqlalchemy +orm(1):链接mysql 数据库

1、pycharm中新建一个flask项目

2、按装flask、PyMySQL、flask-sqlalchemy

3、项目下面新建一个config.py 文件

DEBUG = True
#dialect+driver://root:1q2w3e4r5t@127.0.0.1:3306/
DIALECT = 'mysql'
DRIVER='pymysql'
USERNAME = 'root'
PASSWORD = '1q2w3e4r5t'
HOST = '127.0.0.1'
PORT = 3306
DATABASE = 'db_demo1'

SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False
print(SQLALCHEMY_DATABASE_URI)

4、app.py 文件

from flask import Flask
import config

from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object(config)

db = SQLAlchemy(app)
db.create_all()



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



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

执行app.py 文件,结果如下,表面执行成功

FLASK_APP = test_sqlalchemy.py
FLASK_ENV = development
FLASK_DEBUG = 1
In folder /Users/autotest/PycharmProjects/python3_flask
/Users/autotest/PycharmProjects/python3_flask/venv/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --module --multiproc --qt-support=auto --client 127.0.0.1 --port 55365 --file flask run
pydev debugger: process 3089 is connecting

Connected to pydev debugger (build 182.4505.26)
* Serving Flask app "test_sqlalchemy.py" (lazy loading)
* Environment: development
* Debug mode: on
mysql+pymysql://root:1q2w3e4r5t@127.0.0.1:3306/db_demo1?charset=utf8
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
pydev debugger: process 3090 is connecting

mysql+pymysql://root:1q2w3e4r5t@127.0.0.1:3306/db_demo1?charset=utf8
* Debugger is active!
* Debugger PIN: 216-502-598

欢迎扫描二维码关注技术公众号:为测,上面有自动化测试方面技术文章

 

posted @ 2018-11-07 17:57  testway  阅读(1539)  评论(0编辑  收藏  举报