我从前端接收文本字段的值并对这些值执行 sql 操作。我正在使用“insert_format.format”来格式化字段。但这在从 UI 发送元字符时不起作用。
下面是我的python代码:
insert_string_template = ''' INSERT INTO SELF_SERVICE_ALARMS.metric_definition
(metric_id,
login,
metric_name,
schema_name,
metric_object,
metric_column_name
)
VALUES'''
insert_format = "(nextval ('metric_id_seq'),'" + getpass.getuser() + "','{}','{}','{}','{}')"
当有人输入单引号时,这不起作用。例如 :
如果用户在 metric_name 中输入“ biswajit's ”规则,它会扩展为
INSERT INTO SELF_SERVICE_ALARMS.metric_definition
(metric_id,
login,
metric_name,
schema_name,
metric_object,
metric_column_name
)
VALUES
nextval ('metric_id_seq') , 'xxx', 'biswajit's rule', 'yyy' , 'zzz' ,'aaa') ```
The query fails because of the single quote in 'biswajit's rule'.
Is there a way to fix this issue in insert_format ??