mirror of https://gitlab.com/pamhyr/pamhyr2
SQL: Minor change and add exception handler.
parent
c9887a97e7
commit
7f8e8d5a9f
|
|
@ -177,6 +177,13 @@ class Study(SQLModel):
|
||||||
# SQL #
|
# 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):
|
def _create(self):
|
||||||
# Info (metadata)
|
# Info (metadata)
|
||||||
self.execute(
|
self.execute(
|
||||||
|
|
|
||||||
12
src/tools.py
12
src/tools.py
|
|
@ -312,19 +312,29 @@ class SQL(object):
|
||||||
|
|
||||||
def _db_format(self, value):
|
def _db_format(self, value):
|
||||||
# Replace ''' by ''' to preserve SQL injection
|
# Replace ''' by ''' to preserve SQL injection
|
||||||
if value is str:
|
if type(value) is str:
|
||||||
value = value.replace("'", "'")
|
value = value.replace("'", "'")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@timer
|
@timer
|
||||||
def execute(self, cmd, fetch_one=True, commit=False):
|
def execute(self, cmd, fetch_one=True, commit=False):
|
||||||
logger.debug(f"SQL - {cmd}")
|
logger.debug(f"SQL - {cmd}")
|
||||||
|
|
||||||
|
value = None
|
||||||
|
try:
|
||||||
res = self._cur.execute(cmd)
|
res = self._cur.execute(cmd)
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
self._db.commit()
|
self._db.commit()
|
||||||
|
|
||||||
value = self._fetch(res, fetch_one)
|
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
|
return value
|
||||||
|
|
||||||
def _create(self):
|
def _create(self):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue