Preparation for centralized queries

To handle exceptions, etc.
This commit is contained in:
angelblue05 2016-06-28 17:24:12 -05:00
parent 16ea2e35c3
commit c0f0a1978f
1 changed files with 54 additions and 1 deletions

View File

@ -99,7 +99,7 @@ def kodiSQL(media_type="video"):
else:
dbPath = getKodiVideoDBPath()
connection = sqlite3.connect(dbPath)
connection = sqlite3.connect(dbPath, timeout=20)
return connection
def getKodiVideoDBPath():
@ -134,6 +134,59 @@ def getKodiMusicDBPath():
% dbVersion.get(xbmc.getInfoLabel('System.BuildVersion')[:2], "")).decode('utf-8')
return dbPath
def querySQL(query, args=None, cursor=None, conntype=None):
result = None
manualconn = False
failed = False
if cursor is None:
if conntype is None:
log("New connection type is missing.", 1)
return result
else:
manualconn = True
connection = kodiSQL(conntype)
cursor = connection.cursor()
attempts = 0
while attempts < 3:
try:
log("Query: %s Args: %s" % (query, args), 2)
if args is None:
result = cursor.execute(query)
else:
result = cursor.execute(query, args)
break # Query successful, break out of while loop
except sqlite3.OperationalError as e:
if "database is locked" in e:
log("%s...Attempt: %s" % (e, attempts), 0)
attempts += 1
xbmc.sleep(1000)
else:
log("Error sqlite3: %s" % e, 0)
if manualconn:
cursor.close()
raise
except sqlite3.Error as e:
log("Error sqlite3: %s" % e, 0)
if manualconn:
cursor.close()
raise
else:
failed = True
log("FAILED // Query: %s Args: %s" % (query, args), 1)
if manualconn:
if failed:
cursor.close()
else:
connection.commit()
cursor.close()
log(result, 2)
return result
#################################################################################################
# Utility methods