domen
domen

Reputation: 1279

Alternative to filling a database from txt

I have a SQLite database which I fill by reading lines from a txt file inside res/raw/. It works fine, but it takes a few minutes to load everyting in the database, when the app is run for the first time. Is there a faster way to load data in a database?

Upvotes: 1

Views: 68

Answers (1)

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39718

Your best bet is to load it once, then copy the file into the asset folder. From there, you simply copy the database into the correct location when you are trying to create it for the first time. This question shows how to do that.

//Copy the database from assets
private void copyDataBase() throws IOException
{
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer))>0)
    {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

Upvotes: 2

Related Questions