本文实例讲述了Python使用Flask-SQLAlchemy连接数据库操作。分享给大家供大家参考,具体如下:
需要安装flask
安装Mysql-Python (这个是py的mysql驱动,这个在官方没有win的支持,只有第三方才有py2.7的whl)
注:上述whl文件也可点击此处链接下载到本地安装:https://www.lfd.uci.edu/~gohlke/pythonlibs/
安装 Flask-SQLAlchemy
注意,如果出现了编码问题,安装的时候,有可能是终端的编码有问题,我换成了git bash shell
就没问题了。
myflask.py
config.py
运行看控制台:(有一些无关紧要的警告,可以不管)
D:/Python27/python.exe D:/PythonProjects/learn0/myflask.py
D:/Python27/lib/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
D:/Python27/lib/site-packages/sqlalchemy/engine/default.py:470: Warning: Incorrect string value: '/xD6/xD0/xB9/xFA/xB1/xEA...' for column 'VARIABLE_VALUE' at row 478
cursor.execute(statement, parameters)
* Restarting with stat
D:/Python27/lib/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
D:/Python27/lib/site-packages/sqlalchemy/engine/default.py:470: Warning: Incorrect string value: '/xD6/xD0/xB9/xFA/xB1/xEA...' for column 'VARIABLE_VALUE' at row 478
cursor.execute(statement, parameters)
* Debugger is active!
* Debugger PIN: 164-312-281
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [18/Oct/2017 16:01:03] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/Oct/2017 16:01:04] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [18/Oct/2017 16:01:06] "GET / HTTP/1.1" 200 -
希望本文所述对大家基于flask框架的Python程序设计有所帮助。
新闻热点
疑难解答