Skip to content

Commit

Permalink
Only create SQLite connection when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rall3n committed Sep 3, 2020
1 parent 764ba1c commit ce976c6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/db_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sqlite3

from contextlib import closing

class DBClient:
def __init__(self, path):
self.path = path
self.con = sqlite3.connect(path)
self.con.row_factory = sqlite3.Row

def select(self, table, rows=['*'], where='1'):
cursor = self.con.cursor()
cursor.execute(f'SELECT {", ".join(rows)} FROM {table} WHERE {where};')
return cursor.fetchall()
with closing(sqlite3.connect(self.path)) as conn:
conn.row_factory = sqlite3.Row
with closing(conn.cursor()) as cursor:
cursor.execute(f'SELECT {", ".join(rows)} FROM {table} WHERE {where};')
return cursor.fetchall()

0 comments on commit ce976c6

Please sign in to comment.