定义的表结构,同时也是print_order对象
print_order = Table('print_order', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('order_id', String),
Column('Ten_or_Ali_order_id', String),
Column('payment_state', String),
Column('fee', String),
Column('print_num', String),
Column('consignee_name', String),
Column('consignee_phone', String),
Column('consignee_addr', String),
Column('origin_pic_name', String),
Column('final_pic_name', String),
Column('print_pic_name', String),
Column('origin_pic_name', String),
Column('spec_name', String),
Column('serial_number', String),
Column('payment_time', String)
)
然后在查询的时用的
sqlalchemy
,那么分页的的写法很简单,就是limit().offset()
# 根据手机号查
def searchPrintOrderWithPhoneNumber(self, phoneNum):
pattern = re.compile(r"0?(13|14|15|16|17|18)[0-9]{9}")
phone_str = pattern.match(phoneNum)
if phone_str:
s = select([print_order]).limit(10).offset(2)
sqlResult = self.conn.execute(s).fetchall()
print sqlResult
return phone_str.group()
else:
phone_str = '请输入正确的号码'
return phone_str
这是句就是查询和分页``` s = select([print_order]).limit(10).offset(2)
`limit(10)`限制返回10条,`offset(20)`表示偏移20个位置,从第21条开始返回。
比如手机上每页显示10条数据,那么这时候就是从第三页开始显示。`limit()`和`offset()`里头的参数让前端传过来就可以了。
#####如果开发者SQL基本功较好的话,直接在代码中写SQL,然后利用session.execute()来执行就更直接了
根据手机号查
def searchPrintOrderWithPhoneNumber(self, phoneNum):
pattern = re.compile(r"0?(13|14|15|16|17|18)[0-9]{9}")
phone_str = pattern.match(phoneNum)
if phone_str:
sql = "SELECT * FROM print_order WHERE payment_state = 1 AND consignee_phone = " + phoneNum + " ORDER BY id DESC LIMIT 10;"
sqlResult = self.session.execute(sql)
for print_order in sqlResult:
print print_order.consignee_phone
return phone_str.group()
else:
phone_str = '请输入正确的号码'
return phone_str
`end`