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

发布于 2024-12-09 15:47:07 字数 1126 浏览 1 评论 0原文

我有一些我一直在玩的示例代码。基本上我只是从 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");
}

i have some sample code i have been playing around with. Basically im just reading from a csv file and writing into a table. Can the below be improved so that it goes faster seems quite slow

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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-12-16 15:47:07

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

Yes, using SqliteDatabase.beginTransaction() with setTransactionSuccessful() and endTransaction() you can combine several queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文