如何将脚本文件中的所有插入语句存储到 Arraylist 中
How to store all insert statements from the script file into Arraylist
我有 sql 脚本文件,其中包含如下所有插入语句
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES
(1, 'Madhava'),
(2, 'Rao'),
(3, 'Tmr');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES
(1, 'Bangalore'),
(2, 'Chirala'),
(3, 'Pune');
如何将每个语句单独存储到 ArrayList 中,如下所示
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES (1, 'Madhava');
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES (2, 'Rao');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES(1, 'Bangalore');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES(2, 'Chirala');
如何解析 sql 文件并像上面那样将每个语句单独存储到 Arraylist 中?
假设我们有
String data =
" INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES\r\n"
+ " (1, 'Madhava'),\r\n" + " (2, 'Rao'),\r\n"
+ " (3, 'Tmr'); \r\n" + "\r\n"
+ " INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES\r\n"
+ " (1, 'Bangalore'),\r\n" + " (2, 'Chirala'),\r\n"
+ " (3, 'Pune'); ";
List<String> inserts = new ArrayList<>();
Scanner sc = new Scanner(data); //you can also use new Scanner(new File("location/of/your/textfile.sql"));
您的解决方案可能如下所示(我们使用空行将数据分成多个部分):
String command = "";
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty() || command.isEmpty()) {
command = line;
} else {
String values = line.replaceAll(",$", ";");//replace `,` at the end with ;
inserts.add(command + " " + values);
}
}
或者更简单(我们简单地将命令设置为以 "INSERT" 开头的行,并创建命令和非空行的串联):
String command = "";
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
//ignore empty lines
if (!line.isEmpty()) {
if (line.startsWith("INSERT ")) {
command = line;
} else {
//lets change `),` to `);`
String values = line.replaceAll(",$", ";");
inserts.add(command + " " + values);
}
}
}
我有 sql 脚本文件,其中包含如下所有插入语句
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES
(1, 'Madhava'),
(2, 'Rao'),
(3, 'Tmr');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES
(1, 'Bangalore'),
(2, 'Chirala'),
(3, 'Pune');
如何将每个语句单独存储到 ArrayList 中,如下所示
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES (1, 'Madhava');
INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES (2, 'Rao');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES(1, 'Bangalore');
INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES(2, 'Chirala');
如何解析 sql 文件并像上面那样将每个语句单独存储到 Arraylist 中?
假设我们有
String data =
" INSERT INTO PUBLIC.EMPLOYEE(ID, NAME) VALUES\r\n"
+ " (1, 'Madhava'),\r\n" + " (2, 'Rao'),\r\n"
+ " (3, 'Tmr'); \r\n" + "\r\n"
+ " INSERT INTO PUBLIC.ADDRESS(ID, CITY) VALUES\r\n"
+ " (1, 'Bangalore'),\r\n" + " (2, 'Chirala'),\r\n"
+ " (3, 'Pune'); ";
List<String> inserts = new ArrayList<>();
Scanner sc = new Scanner(data); //you can also use new Scanner(new File("location/of/your/textfile.sql"));
您的解决方案可能如下所示(我们使用空行将数据分成多个部分):
String command = "";
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty() || command.isEmpty()) {
command = line;
} else {
String values = line.replaceAll(",$", ";");//replace `,` at the end with ;
inserts.add(command + " " + values);
}
}
或者更简单(我们简单地将命令设置为以 "INSERT" 开头的行,并创建命令和非空行的串联):
String command = "";
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
//ignore empty lines
if (!line.isEmpty()) {
if (line.startsWith("INSERT ")) {
command = line;
} else {
//lets change `),` to `);`
String values = line.replaceAll(",$", ";");
inserts.add(command + " " + values);
}
}
}