1. 程式人生 > >mysql插入字串帶單引號+雙引號

mysql插入字串帶單引號+雙引號

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(’ “這是測試語句”+\’測試單雙引號\’ ‘)