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