溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Python中利用Flask-SQLAlchemy連接數(shù)據(jù)庫

發(fā)布時(shí)間:2021-03-26 16:27:50 來源:億速云 閱讀:232 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了怎么在Python中利用Flask-SQLAlchemy連接數(shù)據(jù)庫,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

需要安裝flask

pip install flask

安裝Mysql-Python (這個(gè)是py的mysql驅(qū)動(dòng),這個(gè)在官方?jīng)]有win的支持,只有第三方才有py2.7的whl)

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

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

安裝 Flask-SQLAlchemy

pipi install Flask-SQLAlchemy

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

myflask.py

#coding:utf-8
from flask import Flask
#安裝 python-mysql 因?yàn)闆]有官方支持win版本,只有網(wǎng)上有whl下載
#pip install flask_sqlalchemy
from flask_sqlalchemy import SQLAlchemy
import config #config.py導(dǎo)入
app = Flask(__name__)
app.config.from_object(config)  #SQLALchemy會(huì)自動(dòng)從配置文件讀取那個(gè)固定的URI字符串
db=SQLAlchemy(app)
db.create_all()
@app.route('/')
def hello_world():
  return '你好世界'
if(__name__=='__main__'):
  app.run(debug=True) #開啟debug模式,這里如果出錯(cuò)會(huì)直接有提示

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'
#這個(gè)連接字符串變量名是固定的具體 參考 flask_sqlalchemy 文檔 sqlalchemy會(huì)自動(dòng)找到flask配置中的 這個(gè)變量
SQLALCHEMY_DATABASE_URI='{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)

運(yùn)行看控制臺(tái):(有一些無關(guān)緊要的警告,可以不管)

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 -

上述內(nèi)容就是怎么在Python中利用Flask-SQLAlchemy連接數(shù)據(jù)庫,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI