SQLAlchemy获取匹配查询的每一行并遍历它们 [英] SQLAlchemy get every row the matches query and loop through them
问题描述
我是Python和SQLAlchemy的新手.我一直在努力从数据库中检索内容,并且每次都可以使用它,但是我有点不确定当select语句返回多行时该怎么做.我尝试使用一些较旧的代码,这些代码在启动SQLAlchemy之前就可以使用,但是db
是SQLAlchemy对象,没有execute()
方法.
I'm new to Python and SQLAlchemy. I've been playing about with retrieving things from the database, and it's worked every time, but im a little unsure what to do when the select statement will return multiple rows. I tried using some older code that worked before I started SQLAlchemy, but db
is a SQLAlchemy object and doesn't have the execute()
method.
application = Applications.query.filter_by(brochureID=brochure.id)
cur = db.execute(application)
entries = cur.fetchall()
然后在我的HTML文件中
and then in my HTML file
{% for entry in entries %}
var getEmail = {{entry.2|tojson|safe}}
emailArray.push(getEmail);
我查看了SQLAlchemy文档,但找不到与获取所有行等效的.first()
.谁能指出我正确的方向?毫无疑问,它很小.
I looked in the SQLAlchemy documentation and I couldn't find a .first()
equivalent to getting all the rows. Can anyone point me in the right direction? No doubt it's something very small.
推荐答案
您的查询是正确的,您只需要更改与结果交互的方式即可.您正在寻找的方法是
Your query is correct, you just need to change the way you interact with the result. The method you are looking for is all()
.
application = Applications.query.filter_by(brochureID=brochure.id)
entries = application.all()
这篇关于SQLAlchemy获取匹配查询的每一行并遍历它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!