我正在尝试用flask做一个允许学生注册的网站。在注册页面上,系统会提示学生输入他们的“chosen_class”,然后将其存储在“studentusers”表中。所以我想要做的是查询表,并获得‘’行的值。
首先,我做了一个flask 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')]
我希望在列表中将这两个类打印出来,而不是一个空的列表
chosen_class
year12b year13
发布于 2019-04-19 02:57:36
如果您想要此查询:
SELECT chosen_class FROM studentusers;
你必须用python编写以下查询:
chosen_classes = StudentUsers.query.with_entities(StudentUsers.chosen_class).all()
这个查询将返回一个元组列表:(‘YARE12B’,),(‘YARE13’,),然后你可以遍历这个列表:
for class in chosen_classes:
print(class[0])
https://stackoverflow.com/questions/55751093
复制相似问题