Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite database on SD card

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);
    }

    ...

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?

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

Thanks

like image 757
user291701 Avatar asked Jan 26 '11 15:01

user291701


People also ask

What is SQLite Sdcard storage DB?

SQLite is an API, a programmer's library, a set of functions your program can call. You build it into your program. If you don't want to call Android's own library. https://developer.android.com/training/data-storage/sqlite. then download the amalgamation source code.

Does SQLite load database into memory?

An SQLite database is normally stored in a single ordinary disk file. However, in certain circumstances, the database might be stored in memory. The most common way to force an SQLite database to exist purely in memory is to open the database using the special filename ":memory:".

Where are SQLite databases stored?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.

Can SQLite store an entire database in a single file?

SQLite packages the entire database into a single file. That single file contains the database layout as well as the actual data held in all the different tables and indexes. The file format is cross-platform and can be accessed on any machine, regardless of native byte order or word size.


1 Answers

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

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

like image 163
Charlie Collins Avatar answered Oct 14 '22 09:10

Charlie Collins