python Flask-SQLAlchemy 连接数据库
2017-10-18 15:53
761 查看
需要安装flask
pip install flask
安装Mysql-Python (这个是py的mysql驱动,这个在官方没有win的支持,只有第三方才有py2.7的whl)
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
安装 Flask-SQLAlchemy
pipi install Flask-SQLAlchemy
注意,如果出现了编码问题,安装的时候,有可能是终端的编码有问题,我换成了git bash shell就没问题了。
myflask.py
#coding:utf-8
from flask import Flask
#安装 python-mysql 因为没有官方支持win版本,只有网上有whl下载
#pip install flask_sqlalchemy
from flask_sqlalchemy import SQLAlchemy
import config #config.py导入
app = Flask(__name__)
app.config.from_object(config) #SQLALchemy会自动从配置文件读取那个固定的URI字符串
db=SQLAlchemy(app)
db.create_all()
@app.route('/')
def hello_world():
return '你好世界'
if(__name__=='__main__'):
app.run(debug=True) #开启debug模式,这里如果出错会直接有提示config.py
#coding:utf-8
#dialect+driver://username:password@host:port/database
DIALECT='mysql'
DRIVER='mysqldb'
USERNAME='root'
PASSWORD='root'
HOST='127.0.0.1'
PORT='3306'
DATABASE='flask0'
#这个连接字符串变量名是固定的具体 参考 flask_sqlalchemy 文档 sqlalchemy会自动找到flask配置中的 这个变量
SQLALCHEMY_DATABASE_URI='{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)运行看控制台:(有一些无关紧要的警告,可以不管)
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 -
pip install flask
安装Mysql-Python (这个是py的mysql驱动,这个在官方没有win的支持,只有第三方才有py2.7的whl)
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
安装 Flask-SQLAlchemy
pipi install Flask-SQLAlchemy
注意,如果出现了编码问题,安装的时候,有可能是终端的编码有问题,我换成了git bash shell就没问题了。
myflask.py
#coding:utf-8
from flask import Flask
#安装 python-mysql 因为没有官方支持win版本,只有网上有whl下载
#pip install flask_sqlalchemy
from flask_sqlalchemy import SQLAlchemy
import config #config.py导入
app = Flask(__name__)
app.config.from_object(config) #SQLALchemy会自动从配置文件读取那个固定的URI字符串
db=SQLAlchemy(app)
db.create_all()
@app.route('/')
def hello_world():
return '你好世界'
if(__name__=='__main__'):
app.run(debug=True) #开启debug模式,这里如果出错会直接有提示config.py
#coding:utf-8
#dialect+driver://username:password@host:port/database
DIALECT='mysql'
DRIVER='mysqldb'
USERNAME='root'
PASSWORD='root'
HOST='127.0.0.1'
PORT='3306'
DATABASE='flask0'
#这个连接字符串变量名是固定的具体 参考 flask_sqlalchemy 文档 sqlalchemy会自动找到flask配置中的 这个变量
SQLALCHEMY_DATABASE_URI='{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)运行看控制台:(有一些无关紧要的警告,可以不管)
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 -
相关文章推荐
- python---pymsql+flask_sqlalchemy连接数据库
- Python使用Flask-SQLAlchemy连接数据库操作示例
- Python的Flask框架与数据库连接的教程
- Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
- Python 语言 Oracle 数据库基于 SQLAlchemy + Flask + Ajax 的 Json 前后端分离开发博客系统遇到的坑汇总
- flask框架在python中通过sqlalchemy+pymysql连接mysql
- Python Flask Web 第十一课 —— 使用 Flask-SQLAlchemy 管理数据库
- Python Flask 中用 SQLAlchemy 访问 Mysql 数据库
- Flask入门之SQLAlchemy数据库连接操作(第15讲)
- python使用 flask_sqlalchemy连接mysql时出现ImportError: No module named MySQLdb错误
- python Flask-SQLAlchemy操作数据库
- flask-SQLALCHEMY 远程连接数据库
- Python安装&flask安装&连接数据库
- SQLAlchemy:python数据库连接 神器
- python3之flask框架连接redis数据库
- Flask入门之SQLAlchemy配置与数据库连接
- Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
- Flask-sqlalchemy连接数据库
- python连接数据库
- python连接数据库