当前位置:网站首页>(07) flask is OK if you have a hand -- flask Sqlalchemy
(07) flask is OK if you have a hand -- flask Sqlalchemy
2022-07-27 12:23:00 【Free little ice】
zero 、 Preface
This article , Elaborate on Flask in Database ORM Use

install
Use it directly pip install flask-sqlalchemy that will do
Use
initialization
Use ORM So that we don't have to write complicated SQL sentence
Database we use Python Self contained sqlite3 This does not require installation .
establish exts.py file , Write the following :
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
The code above means : Create a SQLAlchemy object
establish models.py file , Write the following :
from exts import db
class User(db.Model):
id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(80),unique=True)
def __repr__(self):
return '<User %s>' % self.username
The code above means : Create a user model , In terms of our operation
Finally create app.py file , Write the following :
from flask import Flask, jsonify
from exts import db
from models import User
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///foo.db"
db.init_app(app)
The code above means :
Combine our database with flask_appBinding .
The address of the database is : In the current directory foo.db file
thus , The initial operation of the database has been completed .
Smart little buddy is about to ask , Why not exts.py and app.py Merged ?
answer : If after the merger , that models.py Will quote SQLAlchemy object , and app.py And from models.py I quote User object , This will cause a circular reference problem .
Additions and deletions
@app.route('/create_table')
def create_table():
db.create_all()
return f" The database has been created successfully "
@app.route('/create/<name>')
def create(name):
user = User(username=name)
db.session.add(user)
db.session.commit()
return f" user {
name} Created successfully "
@app.route('/query/all')
def query_all():
users = User.query.all()
data = {
"all user": str(users)}
return jsonify(data)
@app.route('/query_by/<name>')
def query_by_name(name):
user = User.query.filter_by(username=name).first()
data = {
"user": str(user)}
return jsonify(data)
@app.route('/delete_by/<name>')
def delete_by_name(name):
user = User.query.filter_by(username=name).first()
db.session.delete(user)
db.session.commit()
return " user : Delete successful "
- First you need to visit
create_tableTo create a database - And then you can go through
create/xiaobingCreate a small soldier user - adopt
query_allQuery all users - adopt
delete_by/xiaobingDelete the soldier user
Extended literature
边栏推荐
- Fundamentals of mathematics 02 - sequence limit
- Alibaba cloud RDS exception during pool initialization
- Example of MATLAB dichotomy (example of finding zero point by dichotomy)
- STS download tutorial (the solution cannot be downloaded on the include official website)
- 广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
- matlab二分法例题(用二分法求零点例题)
- 2021-3-23-meituan-regular sequence
- JS-寄生组合式继承
- MySQL数据库主从复制集群原理概念以及搭建流程
- Makefile template
猜你喜欢

Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing

Image segmentation vs Adobe Photoshop (PS)
Several rounds of SQL queries in a database

CMD Chinese garbled code solution

我在英国TikTok做直播电商

20210518-Cuda

go入门篇 (4)

npm踩坑
One article to understand the index of like in MySQL

Iptables firewall
随机推荐
2021-3-23-meituan-regular sequence
A personal blog integrating technology, art and sports.
Docker MySQL Usage Note
JS字符串方法总结
go入门篇 (4)
开关量输入输出模块DAM-5055
npm踩坑
Leetcode 01: t1. sum of two numbers; T1108. IP address invalidation; T344. Reverse string
Common usage of curl command
2021-3-22-directed graph sorting
When the script runs in the background, it redirects the log from the console to its own named file
Go Beginner (4)
CLS 监控告警:实时保障线上服务高可用性
POJ1988_ Cube Stacking
POJ1988_Cube Stacking
严控室外作业时间!佛山住建局发文:加强高温期间建筑施工安全管理
Wechat applet must use interface "suggestions collection"
Go Introduction (2)
go入门篇 (2)
虚拟偶像的歌声原来是这样生成的!