SQLAlchemy query the database data

First, create a model, which generates a table in the database, and then fill in the data, the following

 1 # 定义orm,数据模型
 2 class Test(db.Model):
 3     __tablename__ = 'test'
 4     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
 5     username = db.Column(db.String(80), unique=True)
 6     password = db.Column(db.String(80))
 7     email = db.Column(db.String(120), unique=True)
 8 
 9     def __repr__(self):
10         return '<User %r>' % self.username
11 
12 
13 db.create_all ()
 14  
15 @ app.route ( ' / ' )
 16  DEF index ():
 . 17      # 1. increase 
18 is      ADMIN = the Test (username = ' GUEST5 ' , password = ' GUEST5 ' , = In Email ' GUEST5 @ Example .com ' )
 . 19      db.session.add (ADMIN)   # submit a data 
20 is      guestes = [the Test (username = ' the guest1 ' , password = ' the guest1 ' , = In Email ' [email protected] '),
21                Test(username='guest2', password='guest2', email='[email protected]'),
22                Test(username='guest3', password='guest3', email='[email protected]'),
23                Test(username='guest4', password='guest4', email='[email protected]')]
24     db.session.add_all (guestes)   # submission of a plurality of data 
25      db.session.commit ()
 26 is  
27      return  ' Hello World ' 
28  
29  
30  IF  the __name__ == ' __main__ ' :
 31 is      app.run (Debug = True, Port = ' 6009 ' )

 

0x01: The number of user queries

1 Test.query.count()

0x02: query for all users

1 result = Test.query.all()

0x03: Find field to the specified value of the user

1 result = Test.query.filter(Test.username == 'guest1').first()

0x04: Find the specified field to a user begins a string (according to the beginning of the inquiry)

1 results = Test.query.filter(Test.username.startswith('g')).all()

0x05: Find the specified field to the end user of a string (in accordance with the end of inquiry)

1 results = Test.query.filter(Test.username.endswith('1')).all()

0x06: Find the specified field contains the user of a string (keyword queries)

1 results = Test.query.filter(Test.username.contains('e')).all()

 A big brother blog written in great detail, we can look at: https://blog.csdn.net/jlb1024/article/details/81515155

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11249946.html