add a function to convert a tuple into a dictionary
This commit is contained in:
parent
cfa45fc970
commit
941ebc4a24
13
database.py
13
database.py
@ -66,6 +66,19 @@ class DBConnector:
|
||||
finally:
|
||||
return result
|
||||
|
||||
def _read_dict(self) -> list[dict]:
|
||||
"""Read the cursor as a list of dictionaries. psycopg2 defaults to using a list of tuples, so we want to convert
|
||||
each row into a dictionary before we return it."""
|
||||
cols = [i.name for i in self.cur.description]
|
||||
results = []
|
||||
for row in self.cur:
|
||||
row_dict = {}
|
||||
for i in range(0, len(row)):
|
||||
if row[i]:
|
||||
row_dict = {**row_dict, cols[i]: row[i]}
|
||||
results.append(row_dict)
|
||||
return results
|
||||
|
||||
def read(self, **kwargs) -> list[dict]:
|
||||
"""Read rows from a database that match the specified filters.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user