Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Ormlite, DB location

I'm using Ormlite to persist some of the data from my Android app (running on a Motorola Xoom). By default, the sql database is saved to /data/data/[package name]/databases/[dbname].db. Trouble is, the Xoom is not rooted and therefore users do not have access to the directory where the db is saved (can't copy/backup/edit the content of the db).

I have added some extra code into one of the classes to copy the db from /data to the sd card, which works fine, but realistically I think it should be possible to set the path for where the db is stored. Am I missing something, or is this not possible with Ormlite?

Thanks in advance, C

like image 739
Colin McCann Avatar asked Jul 08 '11 18:07

Colin McCann


2 Answers

You can create the database on the SDCard if you use something like

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
[...]
public DatabaseHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory().getAbsolutePath()
    + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}

This creates the db file in e.g. /mnt/sdcard/Android/data/com.your.app/files/myData.sqlite Pro: saves internal memory Con: DB is not accessible when the SDCard is not available (e.g. when it is connected to the PC) Also, it can be read by anyone which could be pro or con.

Using context.getExternalFilesDir() instead of Environment.getExternalStorageDirectory() will use a location that is specific to your app and that will be cleaned up automatically after an uninstall (and it appears on 2.2 also on app update).

P.S. I read somewhere that this approach might not work on Android versions prior to 2.2(?)

like image 91
koljaTM Avatar answered Oct 14 '22 18:10

koljaTM


You can do something like what the guy in https://stackoverflow.com/a/3911641 did.

When you create your object that will do your db transactions override the getReadableDatabase and getWriteableDatabase to use your custom path. Somthing like this:

@Override
public synchronized SQLiteDatabase getWritableDatabase (){
  return SQLiteDatabase.openDatabase("/sdcard/mydatabase.db", null, SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized SQLiteDatabase getReadableDatabase (){
  return SQLiteDatabase.openDatabase("/sdcard/mydatabase.db", null, SQLiteDatabase.OPEN_READONLY);
}

If I were writing your app I wouldn't put the db on the SD card. If the user is copying data from it the app won't be able to get access to it. I would provide a 'backup' functionality that would create a file on the SD card in something like XML, JSON, or CSV. Then it would be up to the user to backup to remote storage and you know that you will always be able to get to your db. Unless you want to create a temp storage and merge everything over onto the sd card when it became avalible... But that sounds like a lot more work then it's worth. I hope this answered your question!

like image 25
Ethan Avatar answered Oct 14 '22 18:10

Ethan