Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the query syntax of SQLAlchemy

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you the query grammar about SQLAlchemy. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

# simple query

Print (session.query (User). All ()

Print (session.query (User.name, User.fullname) .all ())

Print (session.query (User, User.name) .all ())

# query with conditions

Print (session.query (User). Filter_by (name='user1'). All ())

Print (session.query (User) .filter (User.name = = "user") .all ()

Print (session.query (User) .filter (User.name.like ("user%")) .all ()

# Multi-conditional query

Print (session.query (User) .filter (and_ (User.name.like ("user%"), User.fullname.like ("first%")) .all ())

Print (session.query (User) .filter (or_ (User.name.like ("user%"), User.password! = None) .all ())

# sql filtering

Print (session.query (User) .filter ("id >: id") .params (id=1) .all ()

# Association query

Print (session.query (User, Address) .filter (User.id = = Address.user_id) .all ()

Print (session.query (User) .join (User.addresses) .all ())

Print (session.query (User) .outerjoin (User.addresses) .all ())

# aggregate query

Print (session.query (User.name, func.count ('*'). Label ("user_count")) .group_by (User.name) .all ()

Print (session.query (User.name, func.sum (User.id) .label ("user_id_sum")) .group_by (User.name) .all ()

# subquery

Stmt = session.query (Address.user_id, func.count ('*'). Label ("address_count"). Group_by (Address.user_id). Subquery ()

Print (session.query (User, stmt.c.address_count) .outerjoin ((stmt, User.id = = stmt.c.user_id)) .order_by (User.id) .all ()

# exists

Print (session.query (User) .filter (exists () .where (Address.user_id = = User.id)

Print (session.query (User) .filter (User.addresses.any ()

The following is the mapping class:

Class User (Base):

_ _ tablename__ = "users"

Id = Column ("id", Integer, primary_key=True)

Name = Column ("name", String)

Fullname = Column ("fullname", String)

Password = Column ("password", String)

Addresses = relation ("Address", order_by= "Address.id", backref= "user")

Def _ _ init__ (self, id=None, name=None, fullname=None, password=None, addresses= []):

Self.id = id

Self.name = name

Self.fullname = fullname

Self.password = password

Self.addresses = addresses

Def _ repr__ (self):

Return "" .format (name=self.name, fullname=self.fullname, password=self.password, addresses=self.addresses)

Class Address (Base):

_ _ tablename__ = "address"

Id = Column (Integer, primary_key=True)

Email_address = Column (String, nullable=False)

User_id = Column (Integer, ForeignKey ("users.id"))

# user = relation ("User", backref= "addresses", order_by= "Address.id")

Def _ _ init__ (self, email_address=None):

Self.email_address = email_address

Def _ repr__ (self):

Return "" .format (email_address=self.email_address, user=self.user.name)

These are the syntax of the SQLAlchemy query shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report