New db.query() method, refs #290

This commit is contained in:
Simon Willison 2021-06-21 21:03:59 -07:00
commit 9faeef230b
3 changed files with 31 additions and 28 deletions

View file

@ -359,10 +359,14 @@ class Database:
for table in tables
)
def execute_returning_dicts(self, sql, params=None):
def query(self, sql, params=None):
cursor = self.execute(sql, params or tuple())
keys = [d[0] for d in cursor.description]
return [dict(zip(keys, row)) for row in cursor.fetchall()]
for row in cursor:
yield dict(zip(keys, row))
def execute_returning_dicts(self, sql, params=None):
return list(self.query(sql, params))
def resolve_foreign_keys(self, name, foreign_keys):
# foreign_keys may be a list of strcolumn names, a list of ForeignKey tuples,