Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Last active November 13, 2021 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kissgyorgy/9e519fee57edcac6861164e63286e259 to your computer and use it in GitHub Desktop.
Save kissgyorgy/9e519fee57edcac6861164e63286e259 to your computer and use it in GitHub Desktop.
Python3: load multiple SQLite rows into one NamedTuple object
import sqlite3
from typing import namedtuple
class Environment(NamedTuple):
SLACK_CLIENT_ID: str
SLACK_CLIENT_SECRET: str
SECRET_KEY: str
conn = sqlite3.connect('config.db')
conn.row_factory = sqlite3.Row
cur = conn.execute('SELECT * FROM environment')
res = cur.fetchall()
env = Environment(**{r['name']: r['value'] for r in res})
# env == Environment(SLACK_CLIENT_ID='1234.5678', SLACK_CLIENT_SECRET='akshdfjhafhadf', SECRET_KEY='verysecret')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment