We have a function
def update_cfg(self, alert_name, description, owner, cfg_json, permission= '{"org_ids":[""], "dept_ids":[""]}'):
try:
sql = "UPDATE scheduled_tasks SET json = %s, last_updated_date = %s, description = %s, permission = %s WHERE owner = %s AND scheduled_task_name = %s;"
return self.execute_write_sql(sql, [cfg_json, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
description, permission, owner, alert_name])
except Exception as e:
raise e
The 'Update_cfg' calls the 'execute_write_sql' function
def execute_write_sql(self, sql, values, datasource='localdb'):
try:
self.db_con = self.connect_to_db(datasource)
print('values are'+str(values))
# print('value length is' + len(values))
cursor = self.db_con.cursor()
if len(values) > 0:
cursor.execute(sql, values)
print('1st query is' + str(cursor.query))
cursor.execute()
print('2nd query is' + str(cursor.query))
else:
cursor.execute(sql)
print(sql)
self.db_con.commit()
except Exception as exception:
logging.error("Failed to execute SQL in the function execute_write_sql: " + sql)
print(sql)
self.exception = exception
if self.db_con != None:
self.db_con.rollback()
return False
finally:
if (self.db_con != None):
self.db_con.close()
return True
The 'value' input of the 'execute_write_sql' function is a list:
['{giant json}', '2019-03-25 14:49:36', 'test', '{"org_ids":["xxx"], "dept_ids":[]}', 'abc', 'v2']
This makes the 'cursor.query' returns the sql query with improper '\' signs like this
UPDATE scheduled_tasks SET json = \'{giant json}\', last_updated_date = \'2019-03-25 14:49:36\', description = \'test\', permission = \'{"org_ids":["xxx"], "dept_ids":[]}\' WHERE owner = \'abc\' AND scheduled_task_name = \'v2\';
Ideally we want the sql queries to be without '\'
UPDATE scheduled_tasks SET json = '{giant json}', last_updated_date = '2019-03-25 14:49:36', description = 'test', permission = '{"org_ids":["xxx"], "dept_ids":[]}' WHERE owner = 'abc' AND scheduled_task_name = 'v2';
Is there any easy way to fix this?