-1

I'm super-crazy, new at Android development and hitting a snag. As my previous post hinted at, I've got a 116KB SQLite database with 12 tables of data.

I'm not looking to write to the DB from the App. I'm just looking to pull the data and reference it. I want to pull the data and throw it into either TextView or ListView.

What I've researched is that the data needs to be copied from the Assets to the internal device data. I've seen plenty of pages of copy code for reference. I used this answer and copied the code into a Class to try and copy the database.

I share the same question from him AND a couple others:

  1. Do I need to use OnCreate or OnUpdate if I'm only pulling data? (I don't need to create new tables or update them.)

  2. How can I verify that my database has been copied to my internal memory?

Thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Renzou2k4
  • 33
  • 9

1 Answers1

0

Do I need to use OnCreate or OnUpdate if I'm only pulling data? (I don't need to create new tables or update them.)

You only need to code the onCreate and onUpgrade methods of the SQLiteOpenHelper subclass if using such a sub class (often termed as a Database Helper). You do not need to have such a subclass as you can open databases without.

  • If you do use such a subclass then the onCreate method will only be invoked if the database doesn't exist and when an attempt is made to open the database (e.g. when getWritableDatabase or getReadableDatabase are called). As such if you use an external database that is copied to an appropriate location the onCreate would not be invoked thus the method could be empty but the method must still be overidden.

    • Note that when onCreate is called the database itself has in fact been created but has no tables (not actually true, as it will have a table named sqlite_master and also a table named android_metadata).
  • Again if you use such a subclass then the onUpgrade method will only be invoked if the version number passed to the constructor's super method is greater than the Database's user_version (as stored at offset 60). Again this method can be empty but it must be overidden.

  • onDowngrade is the reverse of onUpgrade in that it will be invoked if the version number passed to the super method is less than the Database's user_version. However, this method does not need to be overridden, but if it is called without being overridden then an SQLIteException will be thrown.


How can I verify that my database has been copied to my internal memory?

There are numerous ways you can check that the database has been copied.

You can verify by trying to see if the file exists (doesn't check that the database is in fact a valid database).

Check to see if the file exists and then read the first 16 bytes to ensure that they are SQLite format 3\000 (if there is a great likelihood that it is a valid sqlite database). Database File Format

Attempt to open the database using one of the SQLiteDatabase methods (obviously not openOrCreateDatabase).

Open the database an then extract the tables from the sqlite_master table using query.

As you see the above methods progressively check more information. You may also wish to utilise the utilities here

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks! I'm curious about what you meant by opening a SQLite Database without using a Database Helper. Again, I'm super new to this, but 99% of all the articles I've found regarding pulling SQLite data all consist of using a DBHelper. If there's a much easier way to pull that data without, please point me to a resource I can read and learn about. Thank you, – Renzou2k4 Mar 19 '18 at 15:28
  • Basically you can use the [SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) openDatabase methods e.g. `SQLiteDatabase mydatabase = SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);` (existing database). An example of copying from assets without a Database Helper is here [How to populate ListView with db in asset folder? ](https://stackoverflow.com/questions/49328656/how-to-populate-listview-with-db-in-asset-folder/49330320?noredirect=1#comment85729306_49330320) – MikeT Mar 19 '18 at 19:00