String needInsertStr= 单双引号的字符串;
String finalStr = needInsertStr.replaceAll("'", "\\\\\'"); //作用等于在单引号前面加上转义符号\
//对于其他特殊字符也是一样
long dateMillions = System.currentTimeMillis();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("insert into ");
stringBuilder.append(tableName);
stringBuilder.append("(uuid,name,metric_id,service_id,script,deleted,created,updated) ");
stringBuilder.append("values(uuid(),'");
stringBuilder.append(name);
stringBuilder.append("','");
stringBuilder.append(name);
stringBuilder.append("','");
stringBuilder.append(serviceId);
stringBuilder.append("','");
stringBuilder.append(finalStr);
stringBuilder.append("',0,");
stringBuilder.append(dateMillions);
stringBuilder.append(",");
stringBuilder.append(dateMillions);
stringBuilder.append(")");
return stringBuilder.toString();
// script字段即是带单双引号的字符串
insert into table(string) values(’ “这是测试语句”+\’测试单双引号\’ ‘)