Commit 7f8e8d5a authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

SQL: Minor change and add exception handler.

Showing with 23 additions and 6 deletions
+23 -6
......@@ -177,6 +177,13 @@ class Study(SQLModel):
# SQL #
#######
def _db_insert_into_info(self, key, value, commit=False):
self.execute(
"INSERT INTO info VALUES " +
f"('{key}', '{self._db_format(value)}')",
commit=commit
)
def _create(self):
# Info (metadata)
self.execute(
......
......@@ -312,20 +312,30 @@ class SQL(object):
def _db_format(self, value):
# Replace ''' by ''' to preserve SQL injection
if value is str:
if type(value) is str:
value = value.replace("'", "'")
return value
@timer
def execute(self, cmd, fetch_one=True, commit=False):
logger.debug(f"SQL - {cmd}")
res = self._cur.execute(cmd)
if commit:
self._db.commit()
value = None
try:
res = self._cur.execute(cmd)
value = self._fetch(res, fetch_one)
return value
if commit:
self._db.commit()
value = self._fetch(res, fetch_one)
except Exception as e:
logger.error(
f"[{Fore.RED}ERROR{Style.RESET_ALL}] " +
f"{Fore.RED}{e}{Style.RESET_ALL}"
)
traceback.print_exc()
finally:
return value
def _create(self):
logger.warning("TODO: Create")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment