将新的CSV数据插入sqlite数据库

时间:2018-09-01 11:41:42

标签: sqlite

我想将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

1 个答案:

答案 0 :(得分:0)

伪代码

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