Preface : SqlAlchemy is different from flask-SqlAlchemy . flask-SqlAlchemy is the result of flask encapsulating SqlAlchemy. Relatively speaking, flask-SqlAlchemy is more useful, but this article will introduce the use of SqlAlchemy in detail .
Businesses involved in SqlAlchemy:
1: Create a model object, and SqlAlchemy connects to the database
# 创建model对象
class Power(Base):
__tablename__ = 'power_info'
id = Column(Integer, primary_key=True, nullable=False)
uid = Column(String(255), nullable=False)
power_name = Column(String(255))
create_time = Column(String(255), nullable=False)
update_time = Column(String(255))
# SqlAlchemy连接数据库
url = f'mysql+mysqlconnector://{
user}:{
password}@{
host}:{
port}/{
database}'
engine = create_engine(url, echo=True,encoding='utf-8', pool_timeout=30)
# echo=True 将原生sql语句打印至控制台
# pool_timeout:设置超时时间
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
# 创建session对象:
session = DBSession()
2: Find data
Basic syntax:
-
Find all data:
data = session.query(Field to be searched/can be the entire model object).filter(condition).all() -
Find a single piece of data data:
data = session.query(field to be searched/can be the entire model object).filter(condition).one()/first() -
More other methods such as: .all() .first() .scalar() .one() .one_or_none() .get() etc. For the difference,
see 【The blogger introduces the differences between these methods in detail】 -
Conditional query:
conditional query has filter_by and filter methods . For the difference between the two, see 【filter_by / filter difference】. After actual use, I personally feel that filter is more useful
# eg:
data = session.query(Power.id).filter(Power.uid == uid).all()
3: Data sorting
Basic syntax: .order_by(sorting condition.desc()) [desc() descending order, asc() ascending order]
# eg:
# .order_by(Power.create_time.desc())
data = session.query(Power.id).filter(Power.uid == uid).order_by(Power.create_time.desc()).all()
4: Data pagination
Basic syntax: .limit(number of items per page).offset((page number - 1) * number of items per page)
#: Be sure to pay attention here: if the statement also contains sorting statements, be sure to put the paging statement in the sorting after the statement
# eg:
# .limit(page_size).offset((page_index - 1) * page_size)
data = session.query(Power.id).filter(Power.uid == uid).order_by(Power.create_time.desc()).limit(page_size).offset((page_index - 1) * page_size).all()
5: Data update
Basic syntax: session.query(model object).filter(modification condition).update({'database update field name': update value})
# eg:
session.query(Power).filter(Power.uid==uid).update(
{
'power_name': power_name, 'update_time': update_time})
session.commit()
session.close()
6: Add data:
Basic syntax: variable = model object (the corresponding value of the field in the table to be added)
# eg:
role = Role(uid=uid, role_name=role_name,create_time=create_time, status=Utils.STATUS)
session.add(role)
session.commit()
7: Delete data:
Basic syntax: session.query(model object).filter(delete condition).delete()
# eg:
session.query(RolePower).filter(RolePower.power_uid == uid).delete()
session.commit()
[It is not easy to write an article, please contact the author if you need to forward it! 】