Skip to content Skip to sidebar Skip to footer

How To Import Pre-made Db To Sqlite Managed By Ormlite

I have a .db file and I want to setup it at first run of my android application. I use OrmLite to manage my database. In that .db file a have about 7000 records and when I want to

Solution 1:

Let say you have database named "prepared.db" and your package name is "com.example.android". This is what I do.

  1. make sure you prepared database is under assets folder.
  2. In the constructor of class that extends OrmLiteSqliteOpenHelper, check whether or not database exist.
  3. If database DID NOT exist, copy db from assets folder

(Both Step 2 and 3 happen inside constructor)

This is code sample and It work for me.

publicclassDatabaseHelperextendsOrmLiteSqliteOpenHelper {

    privatestaticfinalStringDATABASE_NAME="prepared.db";
    privatestaticfinalStringDATABASE_PATH="/data/data/com.example.android/databases/";


    publicDatabaseHelper(Context context) {
        super(context, DATABASE_PATH+DATABASE_NAME, null, DATABASE_VERSION);

        booleandbexist= checkdatabase();
        if (!dbexist) {

            // If database did not exist, try copying existing database from assets folder.try {
                Filedir=newFile(DATABASE_PATH);
                dir.mkdirs();
                InputStreammyinput= mContext.getAssets().open(DATABASE_NAME);
                Stringoutfilename= DATABASE_PATH + DATABASE_NAME;
                Log.i(DatabaseHelper.class.getName(), "DB Path : " + outfilename);
                OutputStreammyoutput=newFileOutputStream(outfilename);
                byte[] buffer = newbyte[1024];
                int length;
                while ((length = myinput.read(buffer)) > 0) {
                    myoutput.write(buffer, 0, length);
                }
                myoutput.flush();
                myoutput.close();
                myinput.close();            
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
    * Check whether or not database exist
    */privatebooleancheckdatabase() {
        booleancheckdb=false;

        StringmyPath= DATABASE_PATH + DATABASE_NAME;
        Filedbfile=newFile(myPath);
        checkdb = dbfile.exists();

        Log.i(DatabaseHelper.class.getName(), "DB Exist : " + checkdb);

        return checkdb;
    }
}

P.S : If the database file size is more than 1mb, error will occur. You can split database to solve such issue.

Solution 2:

If you want to provide the data as an external db, you can dump on or more tables (definition and data) using

sqlite3 prepared.db ".dump mytable" > dump.sql

and import them using

sqlite3 main.db < dump.sql

If you want to setup things programmatically in Java, make sure that you do everything in one single transaction using TransactionManager.callInTransaction, since multiple commits (autocommits) are quite expensive in SQLite.

Post a Comment for "How To Import Pre-made Db To Sqlite Managed By Ormlite"