Android、Ormlite、数据库位置
我正在使用 Ormlite 保存 Android 应用程序(在 Motorola Xoom 上运行)中的一些数据。默认情况下,sql数据库保存到/data/data/[包名]/databases/[dbname].db。问题是,Xoom 没有 root 权限,因此用户无权访问保存数据库的目录(无法复制/备份/编辑数据库的内容)。
我在其中一个类中添加了一些额外的代码,以将数据库从 /data 复制到 SD 卡,效果很好,但实际上我认为应该可以设置数据库存储位置的路径。我是否遗漏了什么,或者 Ormlite 不可能做到这一点?
提前致谢, C
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用类似的内容,您可以在 SDCard 上创建数据库
这会在例如 /mnt/sdcard/Android/data/com.your.app/files/myData.sqlite 中创建 db 文件
Pro:节省内存
缺点:当 SDCard 不可用时(例如,当它连接到 PC 时),无法访问 DB
此外,任何人都可以阅读它,无论是赞成还是反对。
使用 context.getExternalFilesDir() 而不是 Environment.getExternalStorageDirectory() 将使用特定于您的应用程序的位置,并且该位置将在卸载后自动清理(并且它也出现在 2.2 上的应用程序更新中)。
PS 我在某处读到这种方法可能不适用于 2.2(?) 之前的 Android 版本
You can create the database on the SDCard if you use something like
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(?)
您可以执行类似于 https://stackoverflow.com/a/3911641 中的人所做的事情。
当您创建对象时,数据库事务将覆盖 getReadableDatabase 和 getWriteableDatabase 以使用您的自定义路径。事情是这样的:
如果我正在编写你的应用程序,我不会将数据库放在 SD 卡上。如果用户从中复制数据,应用程序将无法访问它。我将提供“备份”功能,以 XML、JSON 或 CSV 等格式在 SD 卡上创建文件。然后,由用户来备份到远程存储,并且您知道您始终能够访问您的数据库。除非你想创建一个临时存储,并在 SD 卡可用时将所有内容合并到 SD 卡上……但这听起来需要做很多工作,但值得。我希望这能回答您的问题!
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:
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!
不,您不能直接访问数据库。您可以使用
adb shell
来cd
到该目录。以下是有关该主题的一些提示:
http://developer.android.com/guide/developing/tools/adb .html#sqlite
我不明白 Ormlite 有何用处。
No, you cannot access to database directly. You can use
adb shell
tocd
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.