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) { } }
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.
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.
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.
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.
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).
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.
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