在SD卡上的SQLite数据库卡上、数据库、SD、SQLite

2023-09-05 08:56:18 作者:▲旧人不复。

我希望开发的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..

感谢

推荐答案

我还没有尝试过做你的描述有,但presumably这是可以做到和可能的工作 - 有几个注意事项。首先,外部存储(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的语境,如用你自己的

 
精彩推荐