SQLAlchemy and sql statement

Create an object db sqlalchemy

increase

New single:

# SQLAlchemy:
db.session.add(user) db.session.commit()
# sql:
inster into 表名 values()

 New batch:

# SQLAlchemy:
db.session.add_all([user1,user2...]),
db.session.commit()
#sql:
inster into 表名 values()

Inquire

all (): Search returns a list of

User.query.all()
sql:
select .. from tb1

fitst (): The first query returns the object

User.query.first()
sql:
select .. from tb1 limit 1

get (): id get the object based on the main key, no primary key exists returns None

User.query.get(2)
sql:
select .. from tb1 where id=2

filter_by: filter query

User.query.filter_by(mobile='15910743133').first()
User.query.filter_by(moblie='15910743133',id=1).first()
sql:
select .. from tb1 where mobile='15910743133' limit 1
select .. from tb1 where mobile='15910743133' and id=1 limit 1

filter: filter query

User.query.filter(User.mobile=='15910743133').first()
sql:
select .. from tb1 where mobile='15910743133' limit 1

or:

from sqlalchemy import or_
User.query.filter(or_(User.mobile=='15910743133',User.name=='itcast')).all()
sql:
select .. from tb1 where mobile='15910743133' or name='itcast'

and:

from sqlalchemy import and_
User.query.filter(and_(User.name != '13911111111',name='itcast' )).all()
sql:
select ..from tb1 where mobile='15910743133' or name='itcast'

or:

from sqlalchemy import not_
User.query.filter(not_(User.mobile == '15910743133')).all()
sql:
select..from tb1 where mobile!='15910743133'

limit: limit data acquisition

User.query.limit(3).all()
sql:
select .. from tb1 limit 3
User.query.offset(2).limit(3).all()
sql:
select .. from tb1 limit 2,3

order_by: Sort

User.query.order_by(User.id).all()  # 正序
User.query.order_by(User.id.desc()).all()  # 倒序
sql:
select .. from tb1 order by user_id
select .. from tb1 order by user_id desc

Combined Query: 

User.query.filter(User.id > 1).order_by(User.id.desc()).offset(5).limit(3).all()
sql:
select ... from .. where user_id > 1 order by user_id desc limit 5,3

 Optimizing queries:

Import LOAD_ONLY sqlalchemy.orm from 
User.query.options (LOAD_ONLY (Field 1, Field ... 2)) filter_by (ID = 1) .first (). 
SQL: 
SELECT field 1, field 2, from tb1 where id = 1 limit 1

Packet aggregation query:

Import FUNC SQLAlchemy from 
db.session.query (Relation.user_id, func.count (Relation.target_user_id)). GROUP_BY (Relation.user_id) .all () 
a record of each neuron progenitor # result is a database list, ancestral each element is the result field corresponding 
SQL: SELECT user_id, COUNT (target_user_id) from Group user_relation by user_id;

Associated with the query:

* Get the associated object properties table data, inert inquiry

* ForeignKey() + relationship

* relationship + primaryjoin

* The first way:

By db.ForeignKey * ( 'database table. Database field name') to the relationship table between the names of the two tables (model class)

* By explicitly to the model class supplementary relationship () type of field attributes, when reading this attribute to be associated with the query

* Relationship type attribute default return list, the list is associated with the class object model

* `Uselist` If you know exactly is one to one relationship between tables, relationship property returns is certainly only one class object model, so in this case you do not want to return to the list, you can use uselist = False, return directly associated class object model

* `Lazy` used to control how to load the associated objects (multiple queries or a join is a query)

* `Select` secondary default table when the query associated with the query associated with the read attribute
*` joined` primary object used in the query statement is completed when directly join the main table data table associated with the query
* `dynamic` dynamic per (You can not use uselist = False Note that when using dynamic) must query the database relationship when acquiring property

* `Backref` dereference

```python
class User():

profile = db.relationship('UserProfile', uselist=False, backref='user')
```

backref refers to the class UserProfile model for a supplemental property associated with the query user, when the user attribute is read UserProfile object when the associated User object will return

* The second way:
* By no relationship db.ForeignKey to clear the table, but to clear the table by using primaryjoin parameters of the relationship between property relations relationship
* be supplemented by a clear relationship to the model type () type field properties, when reading this attribute to be associated with the query

     
#查询手机号为13912345678用户所关注的所有用户
sql:      
select user_basic.user_id, user_relation.target_user_id from user_relation join user_basic on user_relation.user_id=user_basic.user_id where user_basic.mobile='13912345678'
        
from sqlalchemy.orm import load_only, contains_eager
User.query.join(User.follows).options(load_only(User.id),  contains_eager(User.follows).load_only(Relation.target_user_id))

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/yzl666/p/11063675.html