簡體   English   中英

插入時應用凍結(Android SQLite 數據庫)

[英]App freeze while insert (Android SQLite DB)

我正在制作一個小應用程序來讀取一些 txt 文件並將其導入到設備上的 SQLite 數據庫中。
問題是,當我正確讀取和導入文件時,應用程序在執行該方法時凍結。 我想顯示一個進度條或類似的東西,但對於這個問題我無能為力。
這是導入方法代碼:

private void importFilesFromTxt() {
    bd = helper.getWritableDatabase();
    File directorio = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
    File file = new File(directorio, "art01.txt");
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        String[] parts;
        String sql = "delete from Articulos";
        bd.execSQL(sql);
        while ((line = br.readLine()) != null) {
            if (line.length() != 328) {
                parts = line.split("\\#+");
                Articulo arti = new Articulo(Integer.parseInt(parts[0]), parts[1], quitarEspaciosInt(parts[2]), quitarEspaciosInt(parts[3])
                        , convertirDecimal(parts[4]), quitarEspaciosInt(parts[5]), quitarEspaciosInt(parts[6]), quitarEspaciosFloat(parts[7]), quitarEspaciosInt(parts[8]));
                helper.addArticulo(arti);
            }
        }
        bd.close();
        br.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

如果您對數據庫進行多項操作,最好是在不同的線程中運行並嘗試使用事務,

試試這個,

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                bd = helper.getWritableDatabase();
                File directorio = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
                File file = new File(directorio, "art01.txt");
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String line;
                    String[] parts;
                    String sql = "delete from Articulos";
                    bd.execSQL(sql);

                    bd.beginTransaction();
                    
                    while ((line = br.readLine()) != null) {
                        if (line.length() != 328) {
                            parts = line.split("\\#+");
                            Articulo arti = new Articulo(Integer.parseInt(parts[0]), parts[1], quitarEspaciosInt(parts[2]), quitarEspaciosInt(parts[3])
                                    , convertirDecimal(parts[4]), quitarEspaciosInt(parts[5]), quitarEspaciosInt(parts[6]), quitarEspaciosFloat(parts[7]), quitarEspaciosInt(parts[8]));
                            helper.addArticulo(arti);
                        }
                    }
                    
                    br.close();
                    bd.setTransactionSuccessful();
                } catch (IOException e) {
//                    System.out.println(e.toString());
                } catch (Exception e) {
                    
                } finally {
                    bd.endTransaction();
                    bd.close();
                }
                return null;
            }
        };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM