SD 卡上的 SQLite 数据库 [英] SQLite database on SD card

查看:21
本文介绍了SD 卡上的 SQLite 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 sd 卡上创建一个 sqlite 数据库(不想用完用户的内部存储).我熟悉 OpenHelper 模式:

I'm looking to create a sqlite database on the sd card (don't want to use up the user's internal storage). I'm familiar with the OpenHelper pattern:

public DatabaseFoo(Context context) {
    OpenHelper openHelper = new OpenHelper(context);
    mDb = openHelper.getWritableDatabase();
}

private static class OpenHelper extends SQLiteOpenHelper {
    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    ...

所以如果我们想在 SD 卡上创建,我认为我们必须使用:

so if we want to create on the sd card, I think instead we have to use:

public static SQLiteDatabase openOrCreateDatabase (String path, 
      SQLiteDatabase.CursorFactory factory);

但是工厂"参数应该是什么,应该使用什么工厂?

But what is the "factory" argument supposed to be, what factory should be used?

也有点担心如果用户在我的应用程序正在使用时移除 SD 卡会发生什么..

Also a bit worried about what happens if the user removes the SD card while my app is in use..

谢谢

推荐答案

我没有尝试执行您在那里描述的操作,但据推测它可以完成并且可能会起作用——但有一些警告.首先,外部存储(SD 卡)不安全,因此任何其他应用程序或用户都可以对其进行读/写.其次,正如您所指出的,当它被卸载时,数据库就会消失.

I haven't tried to do what you describe there, but presumably it could be done and might work -- with a few caveats. First, the external storage (SD card) is not secure, so any other application, or the user, could read/write to it. Second, as you noted, when it's unmounted the DB goes away.

由于这些缺点,您可能最好尝试使用内部存储数据库(默认),该数据库很小并且可能包含指向外部数据(如图像或文件)的指针——它们本身可以打开外部存储(当外部存储不可用时,具有占位符或其他处理方式).

Because of these disadvantages, you would probably be better off to try to use an internal storage database (the default), that is small and possibly includes pointers to external data (like images or files) -- that themselves can be on the external storage (and that have placeholders, or other handling, when the external storage is not available).

不过,如果您想尝试一下,最好覆盖 getDatabasePath 方法 Contexta>,比如用自己的Application 对象,然后传那个变成一个常规的SQLiteOpenHelper.那么你就不必担心游标工厂(这是可选的,如来源确认 - 所以如果你想走那条路,只需传递 null ).

Still, if you want to try it, you might be better off override the getDatabasePath method of Context, such as with your own Application object, and then pass that into a regular SQLiteOpenHelper. Then you wouldn't have to worry about the cursor factory (which is optional, as the source confirms -- so just pass null if instead you want to go that route).

这篇关于SD 卡上的 SQLite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆