It is pretty much the same, what you probably want to retrieve from psycopg2 the data as an iterator of the results of whatever data you are trying to gather in the format of a ālist
of dict
sā so when the data is transmitted to anvil, the format will already match what the data grid is expecting.
What this means from database speak to python speak is you want a list containing the rows from the database as a result, in the format of a {dictionary
} that contains the values of that row in a format where they key/value pairs are the column names as the keys, and the values are the cells in each row.
So a basic example would look like this if you printed out a few rows:
[
{'time':time_data, 'source':source_data, 'title':title_data, 'link':link_data},
{'time':time_data, 'source':source_data, 'title':title_data, 'link':link_data},
{'time':time_data, 'source':source_data, 'title':title_data, 'link':link_data},
{'time':time_data, 'source':source_data, 'title':title_data, 'link':link_data},
]
I found this link, it seems pretty standard for a python SQL library:
Edit I forgot to add:
The standard way results are gathered from a SQL database in python (not exclusive to psycopg2
) is to execute a command to the database server using a cursor
object, if you are retrieving from, instead of sending data to the database, the cursor object itself is where you will get the results from, either by retrieving it through a cursor.method()
, or directly iterating over the cursor object itself.
For databases that do not have cursors like MySQL, python creates a ācursor-like-objectā (A pseudo-cursor) that acts kind of like a data streaming object, which is a trap some people fall into, closing the connection before reading the cursor results.