国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用Flask-SQLAlchemy連接數據庫操作示例

2020-02-15 22:18:28
字體:
來源:轉載
供稿:網友

本文實例講述了Python使用Flask-SQLAlchemy連接數據庫操作。分享給大家供大家參考,具體如下:

需要安裝flask

pip install flask

安裝Mysql-Python (這個是py的mysql驅動,這個在官方沒有win的支持,只有第三方才有py2.7的whl)

pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

注:上述whl文件也可點擊此處鏈接下載到本地安裝:https://www.lfd.uci.edu/~gohlke/pythonlibs/

安裝 Flask-SQLAlchemy

pipi install Flask-SQLAlchemy

注意,如果出現了編碼問題,安裝的時候,有可能是終端的編碼有問題,我換成了git bash shell就沒問題了。

myflask.py

#coding:utf-8from flask import Flask#安裝 python-mysql 因為沒有官方支持win版本,只有網上有whl下載#pip install flask_sqlalchemyfrom flask_sqlalchemy import SQLAlchemyimport 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/databaseDIALECT='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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜阳县| 南和县| 武强县| 亳州市| 湟中县| 科技| 荆州市| 安西县| 墨江| 建水县| 阳西县| 龙泉市| 东安县| 黄山市| 务川| 莱西市| 镇原县| 房产| 阜阳市| 龙海市| 胶州市| 开封县| 铜鼓县| 沙坪坝区| 华池县| 儋州市| 会昌县| 栾川县| 嘉兴市| 南靖县| 霍城县| 永年县| 盐池县| 新宁县| 巴林左旗| 新宁县| 玉山县| 上杭县| 闸北区| 页游| 满城县|