当前位置:网站首页>Flask web minimalist tutorial (III) - Sqlalchemy (part a)
Flask web minimalist tutorial (III) - Sqlalchemy (part a)
2022-06-30 00:27:00 【Riemann Hypothesis】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details
One 、ORM Model design
stay MTV Architecture ,M Express Model The layer is responsible for interacting with the database ,ORM(Object Relational Mapping) Object relational mapping can map specific models to tables in the database one by one , The attributes of the model object correspond to the fields of the database table one by one ; Through the operation of the model to achieve the operation of the database table
ORM Important characteristics of :
- Based on object-oriented programming idea
- Hardly write SQL, Improve development efficiency
- Support multiple types of database switching
SQLAlchemy
SQLAlchemy It's one that provides SQL Toolkit and object relation mapping (ORM) Third party libraries of tools ,Flask The framework itself does not ORM Functional , Therefore, it is necessary to use Flask-SQLAlchemy Extend to achieve ORM, About Flask-SQLAlchemy You can refer to Official documents
Flask Of ORM The extension needs to be installed separately , But it needs to be installed in advance mysqlclient rely on
# rely on
pip3 install mysqlclient
pip3 install Flask-SQLAlchemy
Flask-SQLAlchemy The configuration to connect to the database is as follows :
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql:// user name : password @ Database address / Database to connect to '
SQLALCHEMY_DATABASE_URI, database URI Unified resource locator , Is a string used to identify the resource name
If you want to configure multiple databases , You can use it this way
SQLALCHEMY_BINDS = {
'mysql': 'mysqldb://localhost/ database ',
'sqlite': 'sqlite:////path/to/ database '
}
ORM Model creation
stay PyCharm Create a new Flask project flask-sqlalchemy
Use ORM The model needs to be bound to Flask object , stay app.py Created in Flask After object , Bind objects to the database
from flask_sqlalchemy import SQLAlchemy
# Configure database connections
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:[email protected]/flask'
# binding Flask object
db = SQLAlchemy(app)
Then the database model is designed in this file User
# Design database model
class User(db.Model):
# Specify the table name
# __tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), nullable=False)
password = db.Column(db.String(256), nullable=False)
birth_date = db.Column(db.Date, nullable=True)
age = db.Column(db.Integer, default=0)
Execute the create command input in the terminal python3 , Create a table or delete a table by executing the following command
from app import db
db.create_all() # Create all tables
db.drop_all() # Delete all tables
Exit the command line , Re enter , Delete first , Then execute the creation , When the table name is not specified, the default table name is sys+ The model name is lowercase
ORM Model field type
| Field type | Field description |
|---|---|
| Integer/Float | Integer types / Floating point type |
| String(size) | character string , adopt max_lenght Specify string length |
| Text | Long text |
| DateTime | Time type , Corresponding datetime |
| Boolean | Boolean type |
| PickleType | Stored as a persistent Python object |
| LargeBinary | Store an arbitrarily large binary data |
边栏推荐
- [daily question 1] traversal of binary tree
- Connection query of SQL Server database
- Quick Pow: 如何快速求幂
- 项目一:部署 LAMP ecshop电商平台
- 自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务
- Solr basic operations 13
- [rust weekly library] Tokei - a utility for statistics of code lines and other information
- Findbugs modification summary
- 利用 CertBot 申请 Let’s Encrypt SSL 证书
- Getting started with qpainter: drawing the chess interface
猜你喜欢

Finding a job in 2022 is the "last lesson" for graduates

Sword finger offer II 035 Minimum time difference

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

Getting started with qpainter: drawing the chess interface

GET 和 POST请求的本质区别是什么?

JS的初步语法

Serpentine matrix (array simulates direction, D represents turning)

Fine grained identification, classification, retrieval data set collation

TwinCAT 3 el7211 module controls Beifu servo

Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
随机推荐
HDCP Paring
swift笔记
浮点数通信
股票网上开户及开户流程怎样?还有,在线开户安全么?
关联性——典型相关分析
GET 和 POST请求的本质区别是什么?
MySQL基础2
请指教在线开户是什么意思?另外想问,现在在线开户安全么?
MySQL Foundation 2
01 backpack problem
Cloud native enthusiast weekly: cool collection of grafana monitoring panels
01背包问题
中小企业签署ERP合同时,需要注意这几点
Solr basic operation 10
[rust weekly library] Tokei - a utility for statistics of code lines and other information
学位论文的引用
JS draw polar color gradient
微信支付 APP端 第三弹 申请退款
Solr basic operations 9
Summary of DOM knowledge points