dimanche 28 février 2016

Importing large data into sqlite db on android - takes too long. need alternatives

I have an sqlite table with my app...but will be needing to add/remove large sets of data as quickly as possible. I'm open to a lot of options. Right now, I have about 35,000 lines which I can update in about 15 seconds. I would prefer something even quicker. Essentially need just one column to act as my secondary text.

Right now, the user downloads a txt file and then that file gets passed to the below method.

public boolean installLanguageFile(String resourceId) {

        SQLiteDatabase db;
        db = new DataBaseHelper(getContext()).getWritableDatabase();
        try {

            File mFile = new File(getActivity().getFilesDir() + resourceId);

            InputStream insertsStream = new FileInputStream(mFile);
            BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

            long lengthOfFile = 34504;
            long total = 0;
            int rowId = 1;

            String sql = "UPDATE Story_Data SET text2 = ('?') WHERE _id = ?";
            db.beginTransaction();
            SQLiteStatement stmt = db.compileStatement(sql);
            while (insertReader.ready()) {
                total += rowId;
                stmt.bindString(1, insertReader.readLine());
                stmt.execute();
                stmt.clearBindings();
                publishProgress("" + (rowId * 100 / lengthOfFile));
                rowId++;
            }

            db.setTransactionSuccessful();
            db.endTransaction();

            insertReader.close();

            //mCallback.reloadView();
            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

Aucun commentaire:

Enregistrer un commentaire