如何解析 SQL 查询(字符串格式)并提取 HashMap 中的列名和值?
In:
INSERT INTO CUSTOMERS (ID,NAME,AGE)
VALUES (4, 'Chaitali', 25);
Out:
key "ID", value "4";
key "NAME", value "Chaitali";
key "AGE", value "25";
我想出了以下代码,如果值包含逗号,则该代码不起作用。
HashMap<String, String> map = new HashMap<String, String>();
int startValues = sql.lastIndexOf(" (");
int endValues = sql.length();
String valuesSubstring = sql.substring(startValues +2, endValues-1);
String [] valuesMassive = valuesSubstring.split(",");
int startKeys = sql.indexOf("(");
int endKeys = sql.indexOf(")");
String keysSubstring = sql.substring(startKeys+1, endKeys);
String [] keysMassive = keysSubstring.split(",");
for(int i = 0; i < keysMassive.length; i++) {
map.put(keysMassive[i].trim(), valuesMassive[i]);
}
return map;