SQLite Database on SD Card

I am looking to create a sqlite database on an SD card (I do not want to use the user's internal storage). I am 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); } ... 

therefore, if we want to create on an SD card, I think we should use instead:

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

But what should be the "factory" argument, which factory should be used?

Also a little worried about what happens if the user removes the SD card while my application is using.

thank

+12
android sqlite android-sdcard
Jan 26 2018-11-11T00:
source share
6 answers

I did not try to do what you described there, but perhaps it could be done and could work - with a few caveats. Firstly, external storage (SD card) is not secure, so any other application or user can read / write to it. Secondly, as you noted, when it is unmounted, the DB leaves.

Due to these shortcomings, you probably would be better off trying to use an internal storage database (by default), which is small and possibly includes pointers to external data (such as images or files) that external storage can themselves include ( and which has placeholders or other processing when external storage is unavailable).

However, if you want to try, you might be better off redefining the getDatabasePath Context method, for example, with your own Application object, and then passing this to a regular SQLiteOpenHelper . Then you don't have to worry about the factory cursor (which is optional, as the source confirms - so just pass null if you want to go this route instead).

+9
Jan 26 '11 at 15:44
source share

Do this in your SQLiteOpenHelper constructor:

 DatabaseHelper(Context context) { super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_VERSION); } 

It will create a database in the application folder on sdcard: / sdcard / Android / data / [your_package_name] / files. Thus, the database will be considered as part of the Android application and will be automatically deleted if the user uninstalls the application.

I my app I have a large database, and in most cases it is not suitable for the old phone memory, for example. HTC Desire It works fine on an SD card, and most applications have switched to SDCard anyway, so don’t worry about the inaccessibility of the database, because the application will not be available to it.

+9
Jun 14 2018-12-12T00:
source share

Then create your own flat database β€” most people have very little internal memory, and it annoys them to eat it with a huge database.

And as for the scenario β€œwhat if they delete the SD” - if the user deletes the card, it is obvious that it will not work! Clear. Just make sure that you did not receive an error message when trying to interact with the database, and if you did, just say that the user’s problem has been resolved.

+3
Feb 09 '11 at 16:38
source share

The factory cursor is used to return an instance of your custom cursor implementation. Usually you just use SQLiteCursor, in which case null is passed as the factory argument.

+2
Jan 26 '11 at 15:57
source share

I would recommend not placing the database on an SD card - you will significantly reduce the life of the card, since it has a (large, but still existing) limit on the number of possible records, and quite a few records are required for databases.

+1
Jan 26 '11 at 16:03
source share
 public DataBaseHelper(final Context context) { super(context, Environment.getExternalStorageDirectory() + File.separator+ MYDATABASE_NAME, null, MYDATABASE_VERSION); } 

Also add permission in android manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1
Mar 11 '15 at 6:53
source share



All Articles