Improved SQL INSERT and UPDATE with JSON or dict values.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import datetime, pytz, redis, secrets
|
||||
import datetime, json, pytz, redis, secrets
|
||||
from timeit import default_timer as timer
|
||||
|
||||
from app.config import settings
|
||||
@@ -98,7 +98,7 @@ def sql_connect(
|
||||
|
||||
# ### BEGIN ### API DB SQL ### sql_insert() ###
|
||||
# NOTE: Possible future change... Try to look up a duplicate entry if there is one on INSERT.
|
||||
# Updated 2021-09-07
|
||||
# Updated 2023-02-08
|
||||
@logger_reset
|
||||
def sql_insert(
|
||||
sql: str|None = None,
|
||||
@@ -139,6 +139,10 @@ def sql_insert(
|
||||
for key, value in data.items():
|
||||
if key != 'id': # Creating a special exception for the id field.
|
||||
field_list.append('`'+str(key) + '` = :' + str(key))
|
||||
|
||||
if isinstance(value, dict):
|
||||
data[key] = json.dumps(value)
|
||||
|
||||
set_values_string = ', '.join(field_list)
|
||||
|
||||
sql_insert = text(f"""
|
||||
@@ -233,7 +237,7 @@ def sql_insert(
|
||||
|
||||
|
||||
# ### BEGIN ### API DB SQL ### sql_update() ###
|
||||
# Updated 2021-09-07
|
||||
# Updated 2023-02-08
|
||||
@logger_reset
|
||||
def sql_update(
|
||||
sql: str|None = None,
|
||||
@@ -262,6 +266,9 @@ def sql_update(
|
||||
if key != 'id': # Creating a special exception for the id field.
|
||||
fields_string.append('`'+str(key) + '` = :' + str(key))
|
||||
|
||||
if isinstance(value, dict):
|
||||
data[key] = json.dumps(value)
|
||||
|
||||
sql_set = ', '.join(fields_string)
|
||||
log.debug(sql_set)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user