Android的,Ormlite,DB位置位置、Android、Ormlite、DB

2023-09-06 03:06:57 作者:小骄傲项前进i

我使用Ormlite从我的Andr​​oid应用程序仍然存在的一些数据(在摩托罗拉Xoom运行)。默认情况下,SQL数据库保存到/数据/数据​​/ [包名] /数据库/ [DBNAME] .db的。麻烦的是,Xoom的根源并非是因此用户不能访问到该数据库保存(不能复制/备份/编辑数据库的内容)的目录。

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).

我增加了一些额外的code进入其中一个类从/数据到SD卡,它工作正常复制分贝,但实际上我认为它应该是可以设置的,其中db是路径存储。我缺少的东西,或者这是不可能的Ormlite?

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?

在此先感谢, ç

推荐答案

可以,如果你使用像

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

这将创建例如数据库文件/mnt/sdcard/Android/data/com.your.app/files/myData.sqlite 优点:节省内存 缺点:DB是不可访问时的SD卡不可用(例如,当它被连接到PC)的 此外,它可以由任何人可以是赞成或反对被读取

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.

使用context.getExternalFilesDir(),而不是Environment.getExternalStorageDirectory()将使用一个位置,具体到你的应用程序,这将被卸载后自动清理(和它出现在2.2还对应用程序更新)。

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。我读的地方,这种做法可能无法在Android版本2.2之前的工作(?)

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