当前位置:网站首页>Sqlachemy common operations
Sqlachemy common operations
2022-07-01 14:18:00 【A-L-Kun】
List of articles
SQLAchemy
1、 ORM frame
What is? ORM?
Relational object mapping
class -> surface object -> Record ( A line of data )When there is a corresponding relationship , No longer required SQL sentence , Instead, the operation : class 、 object
pythonThe next commonORMYesdjango orm、SQLAlchemyandpeewee
Concept :
db first: Generate classes according to the tables in the databasecode first: Create database tables from classes
2、 Get to know
2.1 Operation process

2.2 Basic use
stay models.py In file
Create tables and delete tables
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: A.L.Kun
# @file : models.py
# @time : 2022/6/8 0:00
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, INTEGER, String
Base = declarative_base() # Create a base class
# Database connection
engine = create_engine(
"mysql+pymysql://root:[email protected]:3306/flask1?charset=utf8", # database url
max_overflow=0, # Exceeds the maximum number of connections created outside the connection pool size
pool_size=5, # Connection pool size
pool_timeout=30, # There is no thread waiting time in the pool , Otherwise, the report will be wrong
pool_recycle=-1 # How long after a thread in the thread pool has a connection reset
) # By default, the connection pool
# Create table
class Users(Base):
__tablename__ = "users1" # set a table name
id = Column(INTEGER, primary_key=True, autoincrement=True)
name = Column(String(32))
extra = Column(String(32))
# # Create table , If the table already exists , No more
# Base.metadata.create_all(engine)
# # Delete table
# Base.metadata.drop_all(engine)
Add information to the table
Among others py In file
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: A.L.Kun
# @file : test.py
# @time : 2022/6/8 0:13
from sqlalchemy.orm import sessionmaker
import models
session = sessionmaker(bind=models.engine)() # Create connection
obj = models.Users(name="kun", extra="hello")
session.add(obj) # Add data to table
session.commit() # Commit transaction
2.3 Connect to database
SQLAlchemy Can't operate the database itself , Since it has to pymsql Third party plug-ins ,Dialect For and data API To communicate , Call different databases according to different configuration files API, So as to realize the operation of database , Such as :
MySQL-Python
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
pymysql
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
MySQL-Connector
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
cx_Oracle
oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
More on :http://docs.sqlalchemy.org/en/latest/dialects/index.html
2.4 Perform native SQL sentence
# Database connection
engine = create_engine("mysql+pymysql://root:[email protected]:3306/flask1?charset=utf8") # By default, the connection pool
cur = engine.execute("SELECT * FROM user1")
print(cur.fetchall())
3、 Create multiple tables
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: A.L.Kun
# @file : models.py
# @time : 2022/6/8 0:00
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (
create_engine, Column, INTEGER, String,
DATETIME, # Create a column of storage time
ForeignKey, # Foreign key constraints
Index, # Create index
UniqueConstraint, # Create a union unique index
)
import datetime
Base = declarative_base() # Create a base class
# Database connection
engine = create_engine(
"mysql+pymysql://root:[email protected]:3306/flask1?charset=utf8", # database url
) # By default, the connection pool
# Create class table
class Classes(Base):
__tablename__ = "classes" # set a table name
id = Column(INTEGER, primary_key=True, autoincrement=True)
name = Column(String(32), nullable=False, unique=True)
# Student list , It has a one to many relationship with the class schedule
class Student(Base):
__tablename__ = "student"
id = Column(INTEGER, primary_key=True, autoincrement=True)
username = Column(String(32), nullable=False, unique=True)
password = Column(String(64), unique=False)
ctime = Column(DATETIME, default=datetime.datetime.now) # Creation time , Be careful now There is no need to put parentheses after , Otherwise, only the initialization time will be recorded , Not when the data is added
class_id = Column(INTEGER, ForeignKey("classes.id")) # Foreign key constraints
# Students' hobbies
class Hobby(Base):
__tablename__ = "hobby"
id = Column(INTEGER, primary_key=True)
caption = Column(String(50), default=" Basketball ")
from sqlalchemy.orm import relationship
cls = relationship("Classes", secondary="S_H", backref="stus") # Create a multi table Association , adopt cls Key to associate ,secondary It is a table that associates two tables ,backref, Reverse generation
# Many-to-many tables , That is to connect students with their hobbies
class S_H(Base):
__tablename__ = "s2h"
id = Column(INTEGER, primary_key=True, autoincrement=True)
stu_id = Column(INTEGER, ForeignKey("student.id"))
hob_id = Column(INTEGER, ForeignKey("hobby.id"))
__table_args__ = (
UniqueConstraint("stu_id", "hob_id", name="uin_stu_hob"), # Create a joint unique index for the two columns
# Index("in_stu_hob", "stu_id", "extra") # Create an index
)
if __name__ == '__main__':
# Create table , If the table already exists , No more
Base.metadata.create_all(engine)
# # Delete table
# Base.metadata.drop_all(engine)
4、 Additions and deletions
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: A.L.Kun
# @file : test.py
# @time : 2022/6/8 0:13
from sqlalchemy.orm import sessionmaker
import models
from sqlalchemy import text
session = sessionmaker(bind=models.engine)() # Connect to database
def add_data():
""" Add data """
# Single addition
cls = models.Classes(name='1 class ')
session.add(cls)
# Add more
objs = [
models.Classes(name='2 class '),
models.Classes(name='3 class ')
]
session.add_all(objs)
def del_data():
""" Delete data """
session.query(models.Classes).filter(models.Classes.id > 2).delete() # Specify conditions to delete data
def find_data():
""" Query data """
ret = session.query(models.Classes).all() # Get all the data
ret1 = session.query(models.Classes.name).all() # Get name data
ret2 = session.query(models.Classes).filter(models.Classes.name == "2 class 009009").all() # obtain 2 Class data
ret3 = session.query(models.Classes).filter_by(name="2 class 009009").first() # Get the data of class 2
ret4 = session.query(models.Classes).filter(text("id<:value and name=:name")).params(
# :value and :name For a placeholder , Use order_by Sort
value=224,
name="field"
).order_by(models.Classes.id)
ret5 = session.query(models.Classes).from_statement(text("SELECT * FROM classes WHERE name=:name")).params(
name="ed" # structure SQL sentence
)
print(ret5)
# Use subquery
ret6 = session.query(models.Classes).filter(models.Classes.id.in_(
session.query(models.Classes.id).filter_by(name="eee") # It is a subquery
)).all()
ret7 = session.query()
print(ret)
""" Linked table operation """
# Access to student information , And its class information
# Method 1
objs = session.query(models.Student.id, models.Student.username, models.Classes.name).join(models.Classes, isouter=True).all()
# Method 2
objs1 = session.query(models.Student).all()
""" # stay models.Student Add these two lines of code at the end , Automatically associate the database according to the foreign key from sqlalchemy.orm import relationship cls = relationship("Classes", backref="stus") # backref It is a reverse generation , That is, in Classes Add stus = relationship("Student") """
for item in objs1:
print(
item.id,
item.username,
item.cls, # This is the associated database , Can access its internal content
)
def up_data():
""" Modifying data """
session.query(models.Classes).filter(models.Classes.id >= 1).update({
models.Classes.name: models.Classes.name + "009",
# It's fine too "name": models.Classes.name + "009",
},
synchronize_session=False # Do not operate internally , It's a direct splice , If the value is "evaluate" Words , Will perform operations
)
# add_data()
# del_data()
# find_data()
# up_data()
session.commit() # Commit transaction
session.close()
5、 Common operations
# Conditions
# Query by a condition
ret = session.query(Users).filter_by(name='alex').all()
# id > 1 && name == "eric"
ret = session.query(Users).filter(Users.id > 1, Users.name == 'eric').all()
# 1 <= id <= 3 && name == "eric"
ret = session.query(Users).filter(Users.id.between(1, 3), Users.name == 'eric').all()
# id in (1, 3, 4)
ret = session.query(Users).filter(Users.id.in_([1,3,4])).all()
# id not in (1, 3, 4)
ret = session.query(Users).filter(~Users.id.in_([1,3,4])).all()
# SELECT * FROM USERS WHERE id IN (SELECT id FROM USERS WHERE name = "eric")
ret = session.query(Users).filter(Users.id.in_(session.query(Users.id).filter_by(name='eric'))).all()
from sqlalchemy import and_, or_
# id > 3 && name == "eric"
ret = session.query(Users).filter(and_(Users.id > 3, Users.name == 'eric')).all()
# id < 2 || name == "eric"
ret = session.query(Users).filter(or_(Users.id < 2, Users.name == 'eric')).all()
# id < 2 || (name == "eric" && id > 3) || extra
ret = session.query(Users).filter(
or_(
Users.id < 2,
and_(Users.name == 'eric', Users.id > 3),
Users.extra != ""
)).all()
# wildcard
ret = session.query(Users).filter(Users.name.like('e%')).all()
ret = session.query(Users).filter(~Users.name.like('e%')).all()
# Limit
ret = session.query(Users)[1:2]
# Sort
ret = session.query(Users).order_by(Users.name.desc()).all()
ret = session.query(Users).order_by(
Users.name.desc(),
Users.id.asc() #
).all()
# grouping
from sqlalchemy.sql import func
ret = session.query(Users).group_by(Users.extra).all()
ret = session.query(
func.max(Users.id),
func.sum(Users.id),
func.min(Users.id)).group_by(Users.name).all()
ret = session.query(
func.max(Users.id),
func.sum(Users.id),
func.min(Users.id)).group_by(Users.name).having(func.min(Users.id) >2).all()
# Even the table
ret = session.query(Users, Favor).filter(Users.id == Favor.nid).all()
ret = session.query(Person).join(Favor).all()
ret = session.query(Person).join(Favor, isouter=True).all()
# Combine
q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union(q2).all()
q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union_all(q2).all()
边栏推荐
- 自定义注解实现验证信息的功能
- Fiori applications are shared through the enhancement of adaptation project
- When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
- Research Report on the development trend and competitive strategy of the global camera filter bracket industry
- 逻辑是个好东西
- Force deduction solution summary 241- design priority for operation expression
- 算网融合赋能行业转型,移动云点亮数智未来新路标
- Etcd summary mechanism and usage scenarios
- 如何看待国企纷纷卸载微软Office改用金山WPS?
- SQLAchemy 常用操作
猜你喜欢

如何看待国企纷纷卸载微软Office改用金山WPS?

The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
![[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system](/img/b2/e8f81ecda6f5f4fc65501aaf9f13cf.gif)
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system

TDengine 连接器上线 Google Data Studio 应用商店

What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
![[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial](/img/44/b65aaf11b1e632f2dab55b6fc699f6.jpg)
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial

深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障

WebSocket(简单体验版)

被裁三個月,面試到處碰壁,心態已經開始崩了

How can we protect our passwords?
随机推荐
Build your own website (21)
Research Report on the development trend and competitive strategy of the global commercial glassware industry
佩服,阿里女程序卧底 500 多个黑产群……
【剑指 Offer】55 - II. 平衡二叉树
逻辑是个好东西
奔涌而来的数字化浪潮,将怎样颠覆未来?
sqlilabs less10
Play with mongodb - build a mongodb cluster
Force deduction solution summary 241- design priority for operation expression
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障
被裁三个月,面试到处碰壁,心态已经开始崩了
Research Report on the development trend and competitive strategy of the global electromagnetic flowmeter industry
Basic concepts of programming
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
Research Report on the development trend and competitive strategy of the global high temperature label industry
Admire, Ali female program undercover more than 500 black production groups
2022 PMP project management examination agile knowledge points (6)
C语言基础知识
sqlilabs less10