我可以改进此代码以更快地读取和写入数据

时间:2011-10-10 22:03:26

标签: android sqlite bufferedreader

我有一些我一直在玩的示例代码。基本上我只是从csv文件中读取并写入表格。下面可以改进,以便它变得更快似乎很慢

public void impcusdata() throws SQLException, IOException {
    try {

        String UnitID = getUnitID();
        FileReader fr = new FileReader("/mnt/sdcard/CRM.CSV");

        // Put the file into the buffer
        BufferedReader in = new BufferedReader(fr);
        db.execSQL("DELETE FROM " + CUSTOMER_TABLE);
        // Create a new string to hold the data
        String data = "";

        // Create the strings executing SQL commands for the data to be
        // inserted
        String InsertCustString = "INSERT INTO " + CUSTOMER_TABLE
                + " VALUES(";
        String InsertEndString = ");";

        // If there is any data in the .CSV file then read it.
        while ((data = in.readLine()) != null) {
            // String the results to the SQL database
            db.execSQL(InsertCustString + data + InsertEndString);
        }
    } catch (SQLiteException ex) {
        String Shaw = ex.getMessage();

    }
    // log entry to the LogCat file
    Log.v(TAG, "Customer Table has been updated");
}

1 个答案:

答案 0 :(得分:1)

是的,将SqliteDatabase.beginTransaction()setTransactionSuccessful()endTransaction()一起使用,您可以合并多个查询。