I am creating a database helper class and for some of the methods within it I am only querying the database and others I am writing to it.
My understanding is both these methods will open the database up and let the program either just read or write to the database.
For the query statements is it worth just using getReadableDatabase() or is there very little difference in performance between the two methods.
Thanks for your time.
getWritableDatabase() -Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called.
SQLiteOpenHelper class The android. database. sqlite. SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
onUpgrade. Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version. The SQLite ALTER TABLE documentation can be found here.
SQLiteOpenHelper is an in-built class of android. database. sqlite. SQLiteDatabase package. It is a helper class to manage SQLite database creation and version management.
They return the same object unless the disk is full or there is some permission error that forces to open the database in read-only mode. The name is a bit confusing though :)
As a rule of thumb you should put any call to these methods outside the UI thread. Both can take a long time to return.
If you are not going to write the database just use getReadableDatabase
as it will contribute to your code clarity and intention.
More info here.
If you look at NotepadProvider.java in Google's NotePad sample project , you will see that they use both - depending on the use-case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With