3

I have the following but for some reason instead of getting results of my query, I am getting something else

Here is the Python package am using

https://pypi.org/project/databases/

And here is the documentation

https://www.encode.io/databases/database_queries/

from databases import Database
database=Database('postgres://redacted')
await database.connect()
...
...
...
query = "SELECT orders.id AS orders_id, orders.notification_method AS orders_notification_method WHERE shipped=True"
result = await database.fetch_all(query=query))

Here is what am getting instead of getting results of my query

print(result)

[<databases.backends.postgres.Record object at 0x7fb3f415ac50>, <databases.backends.postgres.Record object at 0x7fb3f415ae30>, <databases.backends.postgres.Record object at 0x7fb3f415a470>, <databases.backends.postgres.Record object at 0x7fb3f415af50>, <databases.backends.postgres.Record object at 0x7fb3f415ad70>, <databases.backends.postgres.Record object at 0x7fb3f415ab30>, <databases.backends.postgres.Record object at 0x7fb3f415a7d0>, <databases.backends.postgres.Record object at 0x7fb3f415ae90>]

And type says it is a list

print(type(result))

<class 'list'>

How do I return the actual result of the SQL query which is to return all rows from the query?

What I really want to do

here is sqlalchemy version that works but using databases package not working as mentioned above

What I pretty much want to achieve is to have similar result from sqlalchemy query like below and be able to iterate over the rows from the result of the query

...
...
...
class Orders:
    id: Optional[int]
    notification_method: str
    shipped: Optional[bool]

...
...
...
session = create_session()
result=session.query(Orders).filter(Orders.shipped == True)

print(result)

SELECT orders.id AS orders_id, orders.notification_method AS orders_notification_method FROM orders 
WHERE orders.shipped = true

print(type(result))

<class 'sqlalchemy.orm.query.Query'>

And then I want to be able to iterate over the rows

for r in result:
    print(r)

output

Orders(id=1, notification_method='call', shipped=True)
Orders(id=2, notification_method='sms', shipped=True)
Orders(id=3, notification_method='call', shipped=True)

Just want to get similar result as this sqlalchmey one but using databases as mentioned at beginning of this question

Dharman
  • 30,962
  • 25
  • 85
  • 135
uberrebu
  • 3,597
  • 9
  • 38
  • 73
  • 1
    what is "actual result" if not the `Record`? If I check the source code, `Record` encapsulates the values of each row in `.values` property. What is the problem you have with it? Also you probably want to do [Query](https://www.encode.io/databases/database_queries/#queries) instead of [Raw Query](https://www.encode.io/databases/database_queries/#raw-queries) – yedpodtrzitko May 16 '21 at 07:02
  • i want it to return result of my query...i mean the code shows what i want..i want to print the result of the sql query – uberrebu May 16 '21 at 07:05
  • how do i return the rows from the sql query? – uberrebu May 16 '21 at 07:06
  • i updated question with more context...anything am missing? – uberrebu May 16 '21 at 07:28
  • https://stackoverflow.com/questions/7784148/understanding-repr-function-in-python – Ilja Everilä May 16 '21 at 10:55

1 Answers1

7

databases.backends.postgres.Record is a subclass of the collections.abc.Mapping.

It does contain the result (the "actual result") of the query execution. In order to "extract" the values, you can do the following:

result = await database.fetch_all(query=query))
for rec in result:
    print(tuple(rec.values())  # or you could use `dict(rec.items())` as well

Edit-1: Single result (from comment)

If you execute single query, you should be able to print/convert as per below:

rec = await database.fetch_one(query=query)
print(rec)
print(tuple(rec.values()))
print(dict(rec.items()))

resulting in something similar to:

<databases.backends.postgres.Record object at 0x??????>
(1, 'sms')
{'orders_id': 1, 'orders_notification_method': 'sms'}
van
  • 74,297
  • 13
  • 168
  • 171
  • what if there is a different query that returns a single row and is fetching a single row? like `result = await database.fetch_one(query=query))` how will i get the single dictionary of the single row result? i tried `dict(rec.items())` and got `AttributeError: 'str' object has no attribute 'items'` how do i return the dictionary of that single row for `fetch_one` query? – uberrebu May 16 '21 at 15:08
  • I have updated the answer, but check carefully your code, single query should work as well. – van May 16 '21 at 19:01
  • one other question since you the only one really helpful...celery does not work when i add `async` to the task function...so how do i use the encode databases package with celery? seems databases is only async to query the database with every query ran with `await` and celery does not with `async def function():`...has to be `def function():`...will appreciate your help again..thanks – uberrebu May 16 '21 at 22:14
  • I am sorry, but I do not use celery. But most likely this [question/answer](https://stackoverflow.com/a/43325237/99594) will help you. – van May 17 '21 at 02:11
  • for some reason `res.items` is None for me so I can't call it, if you have this too you can use `dict(res._mapping)`. I used to use `dict(res.items())` but now I got error. why so? – larick Dec 22 '22 at 03:53