slice(start,stop)
方法来做切片操作。也可以使用[start:stop]
的方式来进行切片操作。一般在实际开发中,中括号的形式是用得比较多的。示例代码如下: # coding:utf-8
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
HOSTNAME = '127.0.0.1'
PORT = 3306
DATABASE = 'sqlalchemy_first'
USERNAME = 'root'
PASSWORD = '123456'
DB_URI = 'mysql+pymysql://{username}:{password}@{host}:{port}/{dbname}?charset=utf8'.format(
username=USERNAME, password=PASSWORD, host=HOSTNAME, port=PORT, dbname=DATABASE
)
engine = create_engine(DB_URI)
Base = declarative_base(engine)
Session = sessionmaker(engine)
session = Session()
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(50), nullable=False)
createtime = Column(DateTime, nullable=False, default=datetime.now)
def __repr__(self):
return "Article<title:{}>".format(self.title)
Base.metadata.drop_all()
Base.metadata.create_all()
for _ in xrange(100):
article = Article(title='article{}'.format(_))
session.add(article)
session.commit()
# limit
articles = session.query(Article).limit(10).all()
print articles
# offset
articles = session.query(Article).offset(10).limit(10).all()
print articles
# slice, sliece(start, end) 从start取到end.
articles = session.query(Article).slice(10, 20).all()
print articles
# 切片
articles = session.query(Article)[0:10]
print articles
婆婆去哪儿
婆婆去哪儿
可以吧
为什么