Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use SQLiteOpenHelper over SQLiteDatabase?

Tags:

android

sqlite

In my activity I have for example

SQLiteDatabase db = openOrCreateDatabase(Preferences.DB_NAME, Context.MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, value VARCHAR)"); Cursor dbResult = db.rawQuery("SELECT value FROM data", null); // do sometning with cursors dbResult.close(); db.close(); 

What's the benefit of using SQLiteOpenHelper like

DatabaseHelper helper = new DatabaseHelper(this); SQLiteDatabase db = helper.getWriteableDatabase(); SQLiteDatabase db_2 = helper.getReadableDatabase(); Cursor dbResult = db_2.rawQuery("SELECT value FROM data", null); // do sometning with cursors dbResult.close(); helper.close(); 

Class itself

public class DatabaseHelper extends SQLiteOpenHelper {     public DatabaseHelper(Context context) {         super(context, Preferences.DB_NAME, null, Preferences.DB_VERSION);     }      @Override     public void onCreate(SQLiteDatabase db) {         String query = "CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, value VARCHAR)";         db.execSQL(query);         db.close();     }      @Override     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {     } } 
like image 331
svenkapudija Avatar asked Aug 14 '11 16:08

svenkapudija


People also ask

What is the purpose of SQLiteOpenHelper?

Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

What is difference SQLiteOpenHelper and SQLiteDatabase?

there is no difference between SQLiteOpenHeloper::close() and SQLiteDatabase::close() . SQLiteOpenHeloper::close() is just a wrapper around SQLiteDatabase::close() . but as a rule of thumb, either let SQLiteOpenHelper manages your connections, or don't use it and manage it yourself. see this blog post.

What is SQLiteOpenHelper and Sqlitequerybuilder?

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.

What is the main purpose of creating Databasehelper class in SQLite database?

The recommended approach to using SQLite in an Android app is to create a Database Helper class whose only function is to provide for the creation, modification, and deletion of tables in the database.


2 Answers

SQLiteDatabase

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.

SQLiteOpenHelper

A helper class to manage database creation and version management.

I will say this much, the onUpgrade that comes with SQLiteOpenHelper comes in REALLY handy when upgrading your application. It's mainly for creation and upgrading / version management. SQLiteDatabase is mainly for CRUD operations (you can create with it but that is what SQLiteOpenHelper is for).

like image 171
Jack Avatar answered Sep 21 '22 14:09

Jack


SQLiteOpenHelper provides utilities to simplify the tasks of creating and initialising the database if it's not already created and converting the contents of the database when your application is upgrading and the database schema changes.

If you have a very simple database schema, then it doesn't really get you much, but for anything complicated it's a definite help. It makes sure that all the fiddly edge conditions are covered so that you don't have to, such as putting transactions in all the right places to avoid database corruption.

like image 38
David Given Avatar answered Sep 19 '22 14:09

David Given