no shared connection

This commit is contained in:
j 2014-05-17 11:24:17 +02:00
parent ecf782af64
commit 7443e33270

View file

@ -11,13 +11,15 @@ from settings import covers_db_path
class Covers(dict): class Covers(dict):
def __init__(self, db): def __init__(self, db):
self._db = db self._db = db
def connect(self):
self.conn = sqlite3.connect(self._db, timeout=10)
self.create() self.create()
def connect(self):
conn = sqlite3.connect(self._db, timeout=10)
return conn
def create(self): def create(self):
c = self.conn.cursor() conn = self.connect()
c = conn.cursor()
c.execute(u'CREATE TABLE IF NOT EXISTS cover (id varchar(64) unique, data blob)') c.execute(u'CREATE TABLE IF NOT EXISTS cover (id varchar(64) unique, data blob)')
c.execute(u'CREATE TABLE IF NOT EXISTS setting (key varchar(256) unique, value text)') c.execute(u'CREATE TABLE IF NOT EXISTS setting (key varchar(256) unique, value text)')
if int(self.get_setting(c, 'version', 0)) < 1: if int(self.get_setting(c, 'version', 0)) < 1:
@ -42,34 +44,34 @@ class Covers(dict):
def __getitem__(self, id, default=None): def __getitem__(self, id, default=None):
sql = u'SELECT data FROM cover WHERE id=?' sql = u'SELECT data FROM cover WHERE id=?'
self.connect() conn = self.connect()
c = self.conn.cursor() c = conn.cursor()
c.execute(sql, (id, )) c.execute(sql, (id, ))
data = default data = default
for row in c: for row in c:
data = row[0] data = row[0]
break break
c.close() c.close()
self.conn.close() conn.close()
return data return data
def __setitem__(self, id, data): def __setitem__(self, id, data):
sql = u'INSERT OR REPLACE INTO cover values (?, ?)' sql = u'INSERT OR REPLACE INTO cover values (?, ?)'
self.connect() conn = self.connect()
c = self.conn.cursor() c = conn.cursor()
data = sqlite3.Binary(data) data = sqlite3.Binary(data)
c.execute(sql, (id, data)) c.execute(sql, (id, data))
self.conn.commit() conn.commit()
c.close() c.close()
self.conn.close() conn.close()
def __delitem__(self, id): def __delitem__(self, id):
sql = u'DELETE FROM cover WHERE id = ?' sql = u'DELETE FROM cover WHERE id = ?'
self.connect() conn = self.connect()
c = self.conn.cursor() c = conn.cursor()
c.execute(sql, (id, )) c.execute(sql, (id, ))
self.conn.commit() conn.commit()
c.close() c.close()
self.conn.close() conn.close()
covers = Covers(covers_db_path) covers = Covers(covers_db_path)