The basic framework of the query syntax Flask

The basic syntax:

Model class name .query [. Filters]. Actuators

Commonly used SQLAlchemy query filters

filter Explanation
filter() Add the filter to the original query, the query returns a new
filter_by() Add the equivalent filter to the original query, the query returns a new
limit The value specified defines the original query results returned
offset() Offset from the original query results returned by the query returns a new
order_by() Sort the original query results according to specified conditions, to return a new query
group_by() The original query results are grouped according to specified criteria, returns a new query

Commonly used SQLAlchemy query executor

method Explanation
all() Return query results as a list of all
first() Returns the first result of a query, if not found, return None
first_or_404() Returns the first result of a query, if not found, returns 404
get() Returns the row corresponding to the primary key, if not present, returns None
get_or_404() Returns the row corresponding to the primary key, if not present, returns 404
count() Returns the number of query results
paginate() Returns a Paginate object that contains the results within the specified range

Case Code:

    # Queries all character data 
    Print (PeopleInfo.query.all ()) 
# query how much personal property Print (PeopleInfo.query.count ())
# query first personal matter Print (PeopleInfo.query.first ())
# query id figure 4 is [3 ways] Print (PeopleInfo.query.get (4 )) Print (PeopleInfo.query.filter (PeopleInfo.id == 4 ) .first ()) Print (PeopleInfo.query.filter_by (ID = 4 ) .first ())
# query data for all character names ending g of [start / include] Print (PeopleInfo.query.filter (PeopleInfo.name.endswith ( ' g ' )). All ())
# query the name is not All data is equal to wang [2 ways] Print(PeopleInfo.query.filter (PeopleInfo.name! = ' Wang ' ) .all ())
# query all the data [2 ways] name and email are beginning to li    Print (PeopleInfo.query.filter (PeopleInfo.name. startsWith ( ' Li ' )). filter (PeopleInfo.email.startswith ( ' Li ' )). All ()) Print (PeopleInfo.query.filter (PeopleInfo.name.startswith ( ' Li ' ), PeopleInfo.email.startswith ( ' Li ' )). All ())
# query password is `` email` 123456` or all of the data to the end of `itheima.com` from SQLAlchemy Import or_ Print(PeopleInfo.query.filter (or_ (PeopleInfo.password == ' 123456 ' , PeopleInfo.email.endswith ( ' itheima.com ' ))). All ())
# query id [1, 3, 5, 7, 9] character list Print (PeopleInfo.query.filter (PeopleInfo.id.in _ ([. 1,. 3,. 5,. 7, 9 ])). All ())
# query name is character data liu Print (PeopleInfo.query .filter_by (name = ' liu ' ) .all ())
# queries all character data, and to sort mail # descending print (PeopleInfo.query.order_by (PeopleInfo.email.desc ()). All ()) # ascending print (PeopleInfo.query.order_by (PeopleInfo.email.asc ()). all ())
# Page 3, page 2 of query data # Page = None, the first few pages # per_page = None Per few data paginates = PeopleInfo.query.paginate (Page = 1, per_page = 2 ) # the first few pages print (paginates.page) #  page a few Print (paginates.per_page) # total number of data Print (paginates.total) # data for the current page Print (paginates.items)

Model Class Code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)

# Set URL database connected 
the app.config [ ' SQLALCHEMY_DATABASE_URI ' ] = ' MySQL: // the root: [email protected]: 3306 / Test '

the app.config [ ' SQLALCHEMY_TRACK_MODIFICATIONS ' ] = True
 # will be displayed when the query of the original SQL statement 
the app.config [ ' SQLALCHEMY_ECHO ' ] = True
db = SQLAlchemy(app)

# Define the model 
class BookInfo (db.Model):
     # Set the table name 
    __tablename__ = ' BookInfo ' 
    # after the primary key, automatic growth since 
    the above mentioned id = db.Column (db.Integer, primary_key = True)
    name = db.Column(db.String(20),unique=True,nullable=False)
    pub_date = db.Column(db.Date,nullable=True)
    readcount = db.Column(db.Integer,default=1)
    commentcount = db.Column(db.Integer,default=1)
    is_delete = db.Column(db.Boolean,default=False)


    def __repr__(self):
        return self.name

class PeopleInfo(db.Model):
    __tablename__ = 'peopleinfo'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(20),unique=True)
    password = db.Column(db.String(20),nullable=False)
    email = db.Column(db.String(50),nullable=True)
    #设置外键
    book_id = db.Column(db.Integer,db.ForeignKey('bookinfo.id'))

    def __repr__(self):
        return self.name

if __name__ == '__main__':
    app.run(debug=True)

Guess you like

Origin www.cnblogs.com/chao666/p/12424924.html