Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteDatabase.openDatabase vs SQLiteOpenHelper.getReadableDatabase

Is there any difference between these two methods? Both return an opened SQLiteDatabase. Both can create a database if one doesn't exist. SQLiteOpenHelper also has getWriteableDatabase when read/write is needed...

Which method should I use and where? Based on sample code I've seen, I'm using SQLiteOpenHelper to create my database in the first place, but then calling SQLiteDatabase.openDatabase when I need to use the database.

like image 893
GendoIkari Avatar asked May 31 '11 20:05

GendoIkari


People also ask

What is difference SQLiteOpenHelper and SQLiteDatabase?

If it isn't already exist ofcourse. Difference between SQLiteOpenHelper and SQLiteDatabase close() is that SQLiteOpenHelper just closes inner instance of SQLiteDatabase. And it does it in thread safe way.

What is SQLiteOpenHelper class in Android?

android.database.sqlite.SQLiteOpenHelper. A helper class to manage database creation and version management.

Why do we use class SQLiteOpenHelper?

For maximum control over local data, developers can use SQLite directly by leveraging SQLiteOpenHelper for executing SQL requests and managing a local database.

Which method must be overridden when we extend the SQLiteOpenHelper class?

To use SQLite in Android, a java class should be created as a sub class of SQLiteOpenHelper. This class will act as a database controller which will have the methods to perform the CRUD operations. This custom java class should override the methods named onCreate() and onUpgrade().


1 Answers

The openDatabase() is more flexible allowing you to specify locale etc. but for most circumstances where you don't need to explicitly supply those details the Android documentation says to use getReadableDatabase() and getWriteableDatabase().

like image 77
Gyan aka Gary Buyn Avatar answered Sep 17 '22 15:09

Gyan aka Gary Buyn