我想将csv文件中的新数据插入到我的数据库中。如果任何人可以帮助我使用“file.csv”,我将不胜感激:
NO. from to date
1 antony bits 1/1/2000
2 bits antony 1/2/2000
并且数据库有一个额外的列,看起来像这样
UNO NO. from to date
1 1 antony bits 1/1/2000
2 3 mike antony 1/1/2001
结果可能看起来像:
UNO NO. from to date
1 1 antony bits 1/1/2000
2 3 mike antony 1/1/2001
3 2 bits antony 1/2/2000
伪代码
String[][] data;
// read csv into data
// first row is headers so omit;
for(int r = 1; r < data.length; r++) {
// assuming your database table name is table1
String updateQuery = "update table1 set from = '" + data[r][1] + "', to = '" + data[r][2] + "', date = '" + data[r][3] + "' where NO. = '" + data[r][0] + "'";
int updatedRowCount = executeUpdate(updateQuery);
if(updatedRowCount == 0) {
String insertQuery = "insert into table1 (UNO, NO., from, to, date) values ((select MAX(UNO)+1 FROM table1), '" + data[r][0] + "'" + data[r][1] + "'" + data[r][2] + "'" + data[r][3] + "')";
executeUpdate(insertQuery);
}
}
如果您不能以编程方式执行此操作,则可以使用update并插入上面的sql