赞
踩
ORM模型类也可以使用原生sql
tsql = "select count(c.id) as tcount from course as c where 1=1"
tsql = db.session.execute(tsql)
print(tsql)
此时tsql对象是<sqlalchemy.engine.cursor.CursorResult object at 0x00000000060F0C50>这样一串可循环对象,只要对它进行for循环就可以获取到我们想要的json格式数据了
total = 0
for i in tsql:
total = i[0]
return jsonify({ "total": total})
如果要获取多个对象时只需要把 total = 0 改为 total = [] 再将for循环里的赋值改一下就可以了
total = []
d = {
"id":i[0],
"create_time":i[1],
"update_time":i[2],
"name":i[3],
"url":i[4],
"descrip":i[5],
"tcount":i[6],
"views":i[7],
"is_publish":i[8],
"ctype":i[9]
}
total.append(d)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。