1

I have an android application that uses a database created by SQLite Database Browser as shown in this blog: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

First, I created a database with two tables with one of those tables with one entry, just to check if I was getting the database creation correctly. This worked.

After that, I populated one of the tables with 10 more entries, saved, then copied the database to the assets folder (overwriting the previous one), ran the application, and then queried the 2nd - 10th entries. The application was force closing giving me the CursorIndexOutOfBounds error.

From this, I inferred that the application was somehow unable to use the new database I created and was still using the old one (that was overwritten).

Is there a way to get around and/or fix this? The application seems to be using the first version of the database it receives, even if that database is overwritten by a new one.

Razgriz
  • 7,179
  • 17
  • 78
  • 150

1 Answers1

1

It sounds like your first DB, the one with only a single entry, still exists in your app's database folder under /data/data/your-package-name (on your android device). That tutorial's code will only copy the database from the assets folder if the database does not exist on the device (look at the checkDataBase() method in the tutorial). So, delete the database from the /data/data/your-package-name (either with code or with root access), make sure your updated DB file is in the assets folder, and then run it again. That will ensure the DB from the assets folder is copied onto the device.

Also, make sure the DB_PATH is equal to context.getApplicationInfo().dataDir + "/databases/" instead of "/data/data/YOUR_PACKAGE/databases/".

Jeremy G
  • 568
  • 4
  • 18
  • How do I delete it programmatically? – Razgriz Jan 11 '13 at 00:41
  • Also, when I changed it to the new DB_PATH, it gives a NullPointerException because it was unable to instantiate the activity. – Razgriz Jan 11 '13 at 00:52
  • hmmm.....try the old DB_PATH then. Using context.getApplicationInfo().dataDir is supposed to correct a path error that Jelly Bean users get. It may not be necessary for your purposes. Oh and make sure you are using myContext instead of context or whatever you named your private field. – Jeremy G Jan 11 '13 at 00:57
  • Though I have a feeling that I need to make the context work as the path because I'm having a curious problem about the application not seeing the tables I created in the database. – Razgriz Jan 11 '13 at 01:52
  • Also, thanks for the code you provided for database deletion. I had a function in the helper named `deleteDatabase` that contained that line of code and a function in the main activity (in a button listener) that goes something like: `myDbHelper.deleteDatabase(getApplicationContext());` – Razgriz Jan 12 '13 at 02:45