Forty-one: SQLAlchemy's database of limlt ,, slice, offset and sliced

 

A: limit: limit each query when the query of data of the number of
two: slice: check a range of data, slice (play, stop)
three: offset: the time limit to find the data to filter out front how many
IV: Slice: Yes query object effectively obtain the desired data

 

Ready to work

datetime datetime Import from 

from SQLAlchemy Import create_engine, the Column, Integer, String, the Float, the Text, a ForeignKey, the DateTime
from sqlalchemy.ext.declarative Import declarative_base
from sqlalchemy.orm Import sessionmaker, Relationship, backref

# database information
host = '127.0.0.1'
= Port '3306'
database = 'db_to_sqlalchemy'
username = 'the root'
password = '123456'

widget database # + type connection to the database, pymysql used herein
DB_URI = f'mysql + pymysql: // { username}: {password} Host} {@: {Port} / {Database} '

engine = create_engine (DB_URI) engine # Create
base = declarative_base (engine) # create a base class used declarative_base
session = sessionmaker (engine) ()

class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(50), nullable=False)
create_time = Column(DateTime, nullable=False, default=datetime.now)

def __repr__(self):
return f'title: {self.title}、create_time: {self.create_time}'


Base.metadata.drop_all() # 删除所有表
Base.metadata.create_all() # 创建表

for x in range(100):
article = Article(title=f'title{x}')
session.add(article)
session.commit()

 

A: limit: limit each time the query number of query data, such as finding 10 before

Take 10 before descending (ascending last 10)

 

Two: slice: a check section data, Slice (play, stop), to take the first article 10

 

Three: offset: Find data restrictions when filtering out how many front, such as: data from the beginning of Article 11

Article 11 starts from data, search data 10

Descending take 11-20 article, i.e. article ascending 81-90

 

Four: Slice: effectively obtain the desired data to query objects

 

Guess you like

Origin www.cnblogs.com/zhongyehai/p/11827974.html