当前位置:网站首页>[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
边栏推荐
- 国内数字藏品的乱象与未来
- [deep learning] data preparation -pytorch custom image segmentation data set loading
- OA项目之会议通知(查询&是否参会&反馈详情)
- As long as flutter is data, it will be judged null
- The difference between static library and dynamic library of program
- 程序的静态库与动态库的区别
- NFT 的 10 种实际用途
- The smallest positive number that a subset of an array cannot accumulate
- Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
- 【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
猜你喜欢
![[MySQL] - [subquery]](/img/81/0880f798f0f41724fd485ae82d142d.png)
[MySQL] - [subquery]

Android面试题 | 怎么写一个又好又快的日志库?

NFT 的 10 种实际用途
What are the principles and methods of implementing functional automation testing?

Prometheus and grafana
![[deep learning] data preparation -pytorch custom image segmentation data set loading](/img/7d/61be445febc140027b5d9d16db8d2e.png)
[deep learning] data preparation -pytorch custom image segmentation data set loading
![[WPF] realize language switching through dynamic / static resources](/img/23/1e089ce4a07128323824b25897a8c4.png)
[WPF] realize language switching through dynamic / static resources

Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
![【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna](/img/9a/f857538c574fb54bc1accb737d7aec.png)
【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna

Docker's latest super detailed tutorial - docker creates, runs, and mounts MySQL
随机推荐
Android interview question | how to write a good and fast log library?
Android面试题 | 怎么写一个又好又快的日志库?
[FPGA tutorial case 42] image case 2 - realize image binarization processing through Verilog, and conduct auxiliary verification through MATLAB
[summer daily question] Luogu P6500 [coci2010-2011 3] zbroj
[summer daily question] Luogu p4414 [coci2006-2007 2] ABC
Prometheus与Grafana
LANDSCAPE
写点dp
UPC 小C的王者峡谷
Space shooting Lesson 17: game over (end)
10 practical uses of NFT
《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
[deep learning] data preparation -pytorch custom image segmentation data set loading
Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
String类
stm32 操作W25Q256 W25Q16 spi flash
Go, how to become a gopher, and find work related to go language in 7 days, Part 1
CFdiv1+2-Bash and a Tough Math Puzzle-(线段树单点区间维护gcd+总结)
功能自动化测试实施的原则以及方法有哪些?
The new generation of public chain attacks the "Impossible Triangle"