简体   繁体   English

Select语句仅返回结果的第一行

[英]Select statement only returning first row in result

I have the following code: 我有以下代码:

@staticmethod
def get_all_locations_for_event(value):
   sql = text('SELECT * \
   FROM public.location_event \
   INNER JOIN public.location \
   ON public.location_event.location_id = public.location.id \
   WHERE event_id = :x;')
   result = db.engine.execute(sql, x = value)
   for r in result:
      dict_ = dict(r)
   return dict_

It only returns the first row. 它仅返回第一行。 However I would like it to return all results. 但是,我希望它返回所有结果。 I am new to SQL-Alchemy so forgive me if I am missing something. 我是SQL-Alchemy的新手,如果我缺少什么,请原谅我。 I have searched online for the past couple of hours and so far I have only ran across code samples that return a single row, not multiple rows. 我在过去的几个小时里一直在网上搜索,到目前为止,我只遇到了返回单行而不是多行的代码示例。

I know the issue is with the for loop as it is not iterating enough times to get the rest of the data, however I am not certain what the fix would be seeing result is an object. 我知道问题在于for循环,因为它没有迭代足够的时间来获取其余数据,但是我不确定该看到的结果是对象是什么。

PS If this can be done in an ORM method, I am all ears as well. PS:如果可以通过ORM方法完成此操作,那么我也非常高兴。 I decided to use SQL because I couldn't get my ORM model to select the correct data. 我决定使用SQL,因为无法获得我的ORM模型来选择正确的数据。

Your for loop is iterating just the right amount of times, but in its body you repeatedly rebind the name dict_ to a new dict instance, and so your function returns the very last result produced by your query. 您的for循环仅迭代正确的时间,但是在其主体中,您反复将名称dict_重新绑定到新的dict实例,因此您的函数将返回查询产生的最后结果。 Instead you'll want to return a list, or turn your function to a generator: 相反,您将需要返回列表,或将函数转换为生成器:

def get_all_locations_for_event(value):
    ...
    result = db.engine.execute(sql, x = value)
    return result.fetchall()

For most purposes there is no need to explicitly convert the RowProxy instances of the result to dict , since they act as ordered maps on their own. 在大多数情况下,无需将结果的RowProxy实例显式转换为dict ,因为它们自己充当有序映射。 A notable exception is serializing to JSON, since the json module does not know how to handle SQLAlchemy row proxies. 一个值得注意的例外是序列化为JSON,因为json模块不知道如何处理SQLAlchemy行代理。

I think you're right that the issues lies with your for-loop. 我认为您说对了,问题出在您的for循环上。 It is unclear what you intend dict_ to be but at present, each iteration overwrites it. 目前尚不清楚您打算将dict_设置为什么,但是目前,每次迭代都会覆盖它。 dict_ will hold the values of your result from the final iteration before it exited. dict_将保留结果的值,该值来自最后一次迭代,然后退出。

If I'm not mistaken, your result is already an attributed object (Please correct me if I'm wrong on this) ie you should be able to access the corresponding database columns through the dot notation. 如果我没记错的话,您的结果已经是一个归因对象(如果我错了,请更正我),即您应该能够通过点表示法访问相应的数据库列。 Therefore, you should be able to loop through the items like: 因此,您应该能够遍历以下各项:

Example

dict_ = []

for r in result:
    id = r.id
    name = r.name
    dict_.append((id,name))

I'm guessing you can do something like (untested and depends on your model): 我猜您可以做类似的事情(未经测试,取决于您的模型):

Location.join(LocationEvent).filter_by(event_id=**value**).all()

Join requires relavant foreign key 加入需要相关的外键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM