Java字符串操作 批量转换SQL语句 把Insert转成Delete
一全是数字的:比如把“insertintoSPSTRM.trm_order_accept_control(PRODUCT_ID,SERV_ID,SLA_GRADE,REG...
一全是数字的:比如把“insert into SPSTRM.trm_order_accept_control (PRODUCT_ID, SERV_ID, SLA_GRADE, REGION_ID, DEAL_ID) values (444000011, 5, 200003880, 3, 500000487);
”这句转化成“delete from SPSTRM.trm_order_accept_control where PRODUCT_ID=444000011 and SERV_ID=5 and SLA_GRADE=200003880 and REGION_ID=3 and DEAL_ID=500000487”
二带字符的:比如把“insert into student (a,b,c,d)values(1,'e'2,'f')”这句转化成“delete from student where a=1 and b='e' and c=2 and d='f'”
前辈们帮帮忙!!! 展开
”这句转化成“delete from SPSTRM.trm_order_accept_control where PRODUCT_ID=444000011 and SERV_ID=5 and SLA_GRADE=200003880 and REGION_ID=3 and DEAL_ID=500000487”
二带字符的:比如把“insert into student (a,b,c,d)values(1,'e'2,'f')”这句转化成“delete from student where a=1 and b='e' and c=2 and d='f'”
前辈们帮帮忙!!! 展开
4个回答
展开全部
public class Test {
public static void main(String[] args) {
String sql = "insert into student (a,b,c,d)values(1,'e',2,'f')";
System.out.println(convert(sql));
sql = "insert into SPSTRM.trm_order_accept_control (PRODUCT_ID, SERV_ID, SLA_GRADE, REGION_ID, DEAL_ID) values (444000011, 5, 200003880, 3, 500000487)";
System.out.println(convert(sql));
}
public static String convert(String sql) {
String result = "DELETE FROM ";
sql = sql.toUpperCase();
sql = sql.replace("INSERT INTO ", "");
String table = sql.substring(0, sql.indexOf(" "));
sql = sql.replace(table, "").replace("'", "");
String[] values = sql.split("VALUES");
String[] k = values[0].replace("(", "").replace(")", "").split(",");
String[] v = values[1].replace("(", "").replace(")", "").split(",");
result += table + " WHERE ";
int length = k.length;
for (int i = 0; i < length; i++) {
result += k[i] + "=" + "'" + v[i] + "'";
if (i < length - 1) {
result += " AND ";
}
}
return result;
}
}
public static void main(String[] args) {
String sql = "insert into student (a,b,c,d)values(1,'e',2,'f')";
System.out.println(convert(sql));
sql = "insert into SPSTRM.trm_order_accept_control (PRODUCT_ID, SERV_ID, SLA_GRADE, REGION_ID, DEAL_ID) values (444000011, 5, 200003880, 3, 500000487)";
System.out.println(convert(sql));
}
public static String convert(String sql) {
String result = "DELETE FROM ";
sql = sql.toUpperCase();
sql = sql.replace("INSERT INTO ", "");
String table = sql.substring(0, sql.indexOf(" "));
sql = sql.replace(table, "").replace("'", "");
String[] values = sql.split("VALUES");
String[] k = values[0].replace("(", "").replace(")", "").split(",");
String[] v = values[1].replace("(", "").replace(")", "").split(",");
result += table + " WHERE ";
int length = k.length;
for (int i = 0; i < length; i++) {
result += k[i] + "=" + "'" + v[i] + "'";
if (i < length - 1) {
result += " AND ";
}
}
return result;
}
}
展开全部
这是一些字符串的截取问题,然后再拼接起来。可以用字符串的substring()和split()两个方法处理。
至于字符串类型的值要加单引号这个问题不必纠结,你把所有类型的值也都加上单引号,不会报错且更安全。如把“insert into student (a,b,c,d)values(1,'e'2,'f')”这句转化成“delete from student where a='1' and b='e' and c='2' and d='f'”
至于字符串类型的值要加单引号这个问题不必纠结,你把所有类型的值也都加上单引号,不会报错且更安全。如把“insert into student (a,b,c,d)values(1,'e'2,'f')”这句转化成“delete from student where a='1' and b='e' and c='2' and d='f'”
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把它放到Excel里,利用Excel函数转换!这得你自己探索了!这种方法肯定是行得,只提供方法了!这事我以前干过,加油哦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询