我正在尝试创建一个带有烧瓶的网站,以允许学生注册。在注册页面上,提示学生输入他们的“ chosen_class”,然后将其存储在“ studentusers”表中。所以我想做的就是查询表并在“选择的类”行中获取值。
首先,我进行了烧瓶sqlalchemy查询,并得到一个空列表,然后尝试编写原始sql查询,然后得到了其他内容。
这是我执行sqlalchemy查询时得到的空列表:
p=StudentUsers.query.filter(StudentUsers.chosen_class).all()
>>> p
[]
这是我执行原始SQL查询时得到的:
>>> db.engine.execute('select (chosen_class)' 'from(StudentUsers)')
<sqlalchemy.engine.result.ResultProxy object at 0x0152F770>
>>>
这是表中内容的示例:
StudentUsers('billy','jean','billy','year13', 'billyj@gmail.com','default.jpg')]
year12b 年13
答案 0 :(得分:1)
如果要此查询:
SELECT chosen_class FROM studentusers;
您必须在python中编写以下查询:
chosen_classes = StudentUsers.query.with_entities(StudentUsers.chosen_class).all()
此查询将返回一个元组列表: [('year12b',),('year13',)],然后可以遍历以下列表:
for class in chosen_classes:
print(class[0])