Skip to content Skip to sidebar Skip to footer

Reading Sqlite Database From Android Memory

I have created an sqlite database on a Java application and have pushed it to my android. I want to read this database (probably write into it later too). Most of the tutorials I h

Solution 1:

You must copy the db from assets to your apps storage area (or another accessible location) on the device before you can use it. You cannot use it directly out of the .apk file.

An example of how to go about it:

publicclassDBAdapter { 

// DB info publicstaticfinalStringMAIN_DATABASE_NAME="yourDB"; 
publicstaticStringMAIN_DB_PATH="/data/data/your.package.name/databases/"; 
publicstaticfinalintMAIN_DATABASE_VERSION=1; 

// database control private DatabaseHelper mDbHelper; 
privatestatic SQLiteDatabase mDb; 
privatestatic Context mCtx; 

privatestaticclassDatabaseHelperextendsSQLiteOpenHelper { 
    DatabaseHelper(Context context, String dbname, int dbversion) { 
        super(context, dbname, null, dbversion); 
        if (checkDataBase(dbname)) { 
            openDataBase(dbname); 
        } else { 
            try { 
                this.getReadableDatabase(); 
                copyDataBase(dbname); 
                this.close(); 
                openDataBase(dbname); 
            } catch (IOException e) { 
                thrownewError("Error copying database"); 
            } 
            Toast.makeText(context, 
                    "Initial " + dbname + " database has been created", 
                    Toast.LENGTH_LONG).show(); 
        } 
    } 

    @OverridepublicvoidonCreate(SQLiteDatabase db) { 
    } 

    @OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    } 
} 

publicDBAdapter(Context ctx) { 
    DBAdapter.mCtx = ctx; 
} 

public DBAdapter open(String dbname, int dbversion)throws SQLException { 
    mDbHelper = newDatabaseHelper(mCtx, dbname, dbversion); 
    mDb = mDbHelper.getWritableDatabase(); 
    returnthis; 
} 

publicvoidclose() { 
    mDbHelper.close(); 
} 

privatestaticvoidcopyDataBase(String dbname)throws IOException { 
    InputStreammyInput= mCtx.getAssets().open(dbname); 
    StringoutFileName= MAIN_DB_PATH + dbname; 
    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(); 
} 

privatestaticbooleancheckDataBase(String dbname) { 
    SQLiteDatabasecheckDB=null; 
    booleanexist=false; 
    try { 
        Stringdb= MAIN_DB_PATH + dbname; 
        checkDB = SQLiteDatabase.openDatabase(db, null, 
                SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
        Log.v("db log", "database does't exist"); 
    } 
    if (checkDB != null) { 
        exist = true; 
        checkDB.close(); 
    } 
    return exist; 
} 

privatestaticvoidopenDataBase(String dbname)throws SQLException { 
    StringdbPath= MAIN_DB_PATH + dbname; 
    mDb = SQLiteDatabase.openDatabase(dbPath, null, 
            SQLiteDatabase.OPEN_READWRITE); 
} 
} 

Post a Comment for "Reading Sqlite Database From Android Memory"