当前位置:网站首页>[flask introduction series] installation and configuration of flask Sqlalchemy
[flask introduction series] installation and configuration of flask Sqlalchemy
2022-07-29 07:45:00 【Hall owner a Niu】
Personal profile
- Author's brief introduction : Hello everyone , I'm Daniel , New star creator of the whole stack .
- Blogger's personal website : A Niu's blog house
- Stand by me : give the thumbs-up + Collection ️+ Leaving a message.
- Series column :flask Framework quick start
- Maxim : To be light , Because there are people who are afraid of the dark !

Catalog
Preface
We usually use the drivers that connect to the database with writing sql The pain of , And it is very inconvenient for a large project to use database driver , So choose the right one orm The framework is necessary ! this paper , I will write flask An extension of flask-sqlalchemy Installation and use of !
Flask-SQLAlchemy Introduction of and selection of database driver
SQLAlchemy Is a relational database framework , It provides high-level ORM And the operation of the underlying native database .flask-sqlalchemy It's a simplification SQLAlchemy Operation of the flask Expand .
Speaking of this , A lot of people orm And database drivers are always confused ! Now I'll draw a picture to understand :

You can see that our database driver is responsible for our database from beginning to end mysql Connect !
So use Flask-SQLAlchemy What kind of database driver do we use !
stay python There are three kinds of database drivers commonly used in :
1.MySQLdb
MySQLdb yes Python Connect MySQL One of the most popular drivers , Many frameworks are developed based on this library , Unfortunately, it Only support Python2.x, It is based on C Developed library , and Windows Platform compatibility is not friendly , It is not recommended to use , Instead of a derivative version of it .
2.mysqlclient
because MySQLdb In disrepair , And then there was its Fork edition mysqlclient, Fully compatible with MySQLdb,( Namely support python2), Support at the same time Python3.x, yes Django ORM We rely on tools , If you want to use native SQL To manipulate the database , So we recommend this driver .
3.PyMySQL
PyMySQL Is pure Python The driver of implementation ,== Not as fast as MySQLdb, The biggest feature may be that its installation is not so tedious .== Support python2 and python3!
Many people, including myself, probably use it most often pymysql, But in our flask and django Use in pymysql Do additional operations , take django Come on , The default with MySQLdb, For compatibility python3, We use it MySQLdb The branch of , install mysqlclient, Therefore, no additional operation is required !
If you want to use pymysql, We need to initialize the file in the project __init__.py Write the following code in :
import pymysql
pymysql.install_as_MySQLdb()
about flask, This method is also used , There is another solution :
app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:[email protected]:3306/flask'
That is, the initial agreement is not mysql, It is mysql+pymysql.
thus it can be seen ,pymysql Additional configuration is required , and mysqldb Do not apply python3, Obvious , Our choice of database driver should be consistent with django Same use mysqlclient, Although compared pymysql Less than , But the speed ratio pymysql fast !
And in the flask Medium flask-mysqldb The extension of this connection database is based on mysqlclient Of , We finally use flask-mysqldb!
Flask-SQLAlchemy And flask-mysqldb Installation
pip install flask-sqlalchemy
If the connected database is mysql:
pip install flask-mysqldb
Be careful : If you install directly flask-mysqldb Failure , Please first pip install mysqlclient install mysqlclient !


Flask-SQLAlchemy Configuration of
New in project directory settings.py file , Write the configuration in it , Load from the configuration file !
Those who are not familiar with this area can go to my previous blog :
【flask Introductory series 】Flask Object initialization parameters and Flask Loading method of project configuration
settings.py
# Set the url
SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]:3306/flaskeatproject'
# Dynamic tracking changes settings , If not set, it will only prompt warning , In other words, what you modify in the database will be models Medium sync
SQLALCHEMY_TRACK_MODIFICATIONS = True
These two are enough for common configurations !
app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# From the configuration file settings Load the configuration
app.config.from_pyfile('settings.py')
# Instantiate model class objects , take app Load the database configuration in
db = SQLAlchemy(app)
First, I will show a part here to understand , When explaining the usage later, it will be put into a private folder models In the middle !
You can look at my project directory :

other
frequently-used SQLAlchemy Field type

frequently-used SQLAlchemy Column options

frequently-used SQLAlchemy Relational options

Conclusion
If you think the blogger's writing is good , You can pay attention to the current column , Bloggers will finish this series ! You are also welcome to subscribe to other good columns of bloggers .
Series column
Soft grinding css
Hard bubble javascript
The front end is practical and small demo
边栏推荐
- Dilworth 定理
- OA项目之会议通知(查询&是否参会&反馈详情)
- 【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
- MySQL 45 | 08 is the transaction isolated or not?
- 【暑期每日一题】洛谷 P1601 A+B Problem(高精)
- Segger's hardware anomaly analysis
- State machine DP 3D
- [summer daily question] Luogu p4413 [coci2006-2007 2] R2
- [summer daily question] Luogu p7760 [coci2016-2017 5] tuna
- 状态机dp(简单版)
猜你喜欢
![【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK
What are the principles and methods of implementing functional automation testing?
![[MySQL] - [subquery]](/img/81/0880f798f0f41724fd485ae82d142d.png)
[MySQL] - [subquery]
![【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna](/img/9a/f857538c574fb54bc1accb737d7aec.png)
【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna

MySQL uses date_ FORMAT(date,'%Y-%m')

OA项目之会议通知(查询&是否参会&反馈详情)

【无标题】格式保存

Pat class a 1146 topology sequence

关于大龄读博的几点回答?

QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
随机推荐
State machine DP (simple version)
Strongly connected component
As long as flutter is data, it will be judged null
Docker's latest super detailed tutorial - docker creates, runs, and mounts MySQL
Up sampling deconvolution operation
Prepare esp32 environment
IonIcons图标大全
[summer daily question] Luogu p6320 [coci2006-2007 4] sibice
What are the common error types and solutions of black box testing?
QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
Ionicons icon Encyclopedia
FLink CDC 的mysql connector中,mysql的字段是varbinary, 官方
Why don't you like it? It's easy to send email in cicd
你学习·我奖励,21天学习挑战赛 | 等你来战
Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters
【暑期每日一题】洛谷 P6500 [COCI2010-2011#3] ZBROJ
【暑期每日一题】洛谷 P6320 [COCI2006-2007#4] SIBICE
In JS, 0 means false, and non-0 means true
小D的刺绣
NFT 的 10 种实际用途