ZamoraFTW
ZamoraFTW

Reputation: 37

App freeze while insert (Android SQLite DB)

I am making a little app to read some txt files and import it to a SQLite Database on the device.
The issue is that when I read and import the files properly the app freeze while is doing the method. And I wanna show a progress bar or something similar but with this issue I can´t do nothing.
This is the importing method code:

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

Upvotes: 0

Views: 1393

Answers (1)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

If you are doing multiple operation to the Database, Better is you should run in different thread and also try to use transaction,

Try this,

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

Upvotes: 5

Related Questions