开发者

Android, Ormlite, DB location

开发者 https://www.devze.com 2023-03-18 20:13 出处:网络
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, t

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开发者_如何学JAVA 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


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(?)


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!


No, you cannot access to database directly. You can use adb shell to cd to that directory. Here is some tips on that topic: http://developer.android.com/guide/developing/tools/adb.html#sqlite

And I don't understand what that have with Ormlite.

0

精彩评论

暂无评论...
验证码 换一张
取 消