这个问题在这里已有答案:
我们有一个功能
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
'Update_cfg'调用'execute_write_sql'函数
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
'execute_write_sql'函数的'value'输入是一个列表:
['{giant json}', '2019-03-25 14:49:36', 'test', '{"org_ids":["xxx"], "dept_ids":[]}', 'abc', 'v2']
这使得'cursor.query'返回带有不正确的'\'符号的sql查询
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\';
理想情况下,我们希望sql查询没有'\'
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';
有没有简单的方法来解决这个问题?
通过这个链接启发:
https://www.compose.com/articles/formatted-sql-in-python-with-psycopgs-mogrify/
# very tricky here. We should use 'mogrify' to show us what the real executed query by psycopgs first
new_sql = cursor.mogrify(sql, values)
# The mogrify result suggests that psycopgs returns weird bytes with escapes \\, b\ that messes up the sql syntax
new_sql2 = new_sql.decode("unicode_escape")
# keep this line to make debugging easier locally
new_sql3 = str(new_sql2)
# after decoding, there are still escape symbols here and there. Need to clean them up
new_sql4 = new_sql3.replace("\\", "")
cursor.execute(new_sql4)
# print the real executed sql query in the log file for future debuggin purposes
print('The query is in the execute_write_sql function is ' + str(cursor.mogrify(new_sql4)))