11

Can somebody please give me some links / tutorials to using SQLite file thatis stored on a SD card? Not implementing the OpenHandler.

/SD CARD//info.db

I want to use an external SQlite database file (not use Android internal data store) because I want to frequently update the database file to provide more up to date information on info.db.

Thank you.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
rvpals
  • 177
  • 1
  • 3
  • 15

2 Answers2

18

Have you tried using SQLiteDatabase.openOrCreateDatabase(String, SQLiteDatabase.CursorFactory)? You can simply pass "/sdcard/info.db" as the first and null as the second parameter.

mreichelt
  • 12,359
  • 6
  • 56
  • 70
  • 4
    Good advice, but shouldn't you mention, at least in passing, what will happen when the application can't load the DB because there is no SD card installed? - Amongst the other 2M things that will go wrong :) – KevinDTimm Dec 13 '10 at 20:51
  • 2
    Oh, good point! I totally forgot to mention that the SD card might be unavailable. Perfect that you noted it because my current project also needs to cache data into a SQLite DB on the SD card (if available). – mreichelt Dec 13 '10 at 20:55
  • @mreichelt I did that but still my when installing the app it creates the database on internal storage I also used context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME instead of hardcoding sdcard path but didn't work – Code_Worm Sep 18 '16 at 16:09
8

I would recommend you NOT to put the database on the SD-card due to partly security issues (everyone who has physical access to the device and the SD-card, including all applications running on the device, will also have more or less full access rights to the database file) but also due to hardware issues (an SD-card has a limited lifespan as of the total number of write operations one can perform on it).

These questions are also discussed here: SQLite database on SD card

Community
  • 1
  • 1
dbm
  • 10,376
  • 6
  • 44
  • 56
  • Is that possible to encrypt/ decrypt the db on sdcard. Since the date need to be protected from other apps. – Mahendran Oct 08 '12 at 07:19
  • 1
    It should be possible (technically) since an SQLite database is a file database. Depending on implementation it could affect performance though (e.g. if you for some reason need to decrypt the database for each read operation). – dbm Oct 08 '12 at 08:19
  • Downvoting as a wrong answer, sorry. Especially with the mandatory scoped access in API 30 starting like tomorrow (or one month ago, don't remember) the security is not an issue anymore. "Hardware issues" notion is not an issue either. Any performance degradation is measurable. The problem stated here can be rephrased what to do with the limited internal storage on entry level devices. Please stick to the subject problem, not invent new ones. – halxinate Dec 01 '21 at 21:34
  • @halxinate - thanks for the motivation to your downvote (I really would love if more people followed your example when downvoting in general). I do feel, however, that your tone is uncalled for. Keep in mind that you're commenting on a 10+ year old answer - things have changed since then, everyone knows that. – dbm Dec 08 '21 at 14:51