Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getWritableDatabase() VS getReadableDatabase()

Tags:

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.

like image 300
James Dudley Avatar asked Jul 06 '11 13:07

James Dudley


People also ask

What is getWritableDatabase?

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.

What is SQLiteOpenHelper and Sqlitequerybuilder?

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.

Which method is called when SQLite database needs to be upgraded?

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.

What is SQLiteOpenHelper?

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.


2 Answers

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.

like image 171
mgv Avatar answered Oct 12 '22 02:10

mgv


If you look at NotepadProvider.java in Google's NotePad sample project , you will see that they use both - depending on the use-case.

like image 32
IgorGanapolsky Avatar answered Oct 12 '22 01:10

IgorGanapolsky