-
-
Notifications
You must be signed in to change notification settings - Fork 813
Closed
Labels
Description
I find myself using this pattern all the time:
rows = [dict(r) for r in (await db.execute("select * from t"))]I'm going to add a utility method like this:
rows = (await db.execute("select * from t")).dicts()Relevant code:
datasette/datasette/database.py
Lines 658 to 684 in dc28805
| class Results: | |
| def __init__(self, rows, truncated, description): | |
| self.rows = rows | |
| self.truncated = truncated | |
| self.description = description | |
| @property | |
| def columns(self): | |
| return [d[0] for d in self.description] | |
| def first(self): | |
| if self.rows: | |
| return self.rows[0] | |
| else: | |
| return None | |
| def single_value(self): | |
| if self.rows and 1 == len(self.rows) and 1 == len(self.rows[0]): | |
| return self.rows[0][0] | |
| else: | |
| raise MultipleValues | |
| def __iter__(self): | |
| return iter(self.rows) | |
| def __len__(self): | |
| return len(self.rows) |
Reactions are currently unavailable