Updated 20 February 2016
Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS.
SQLite is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database.
It is embedded in android bydefault. So, there is no need to perform any database setup or administration task.
SQLiteOpenHelper class
SQLiteOpenHelper class provides the functionality to use the SQLite database.
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.
Constructors of SQLiteOpenHelper class
There are two constructors of SQLiteOpenHelper class:
i. SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
creates an object for creating, opening and managing the database.
ii. SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)
creates an object for creating, opening and managing the database. It specifies the error handler.
Methods of SQLiteOpenHelper class:
There are many methods in SQLiteOpenHelper class. Some of them are as follows:
i. public abstract void onCreate(SQLiteDatabase db)
-called only once when a database is created for the first time.
ii. public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
-called when the database needs to be upgraded.
iii. public synchronized void close ()
-closes the database object.
iv. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
-called when the database needs to be downgraded.
SQLiteDatabase class
It contains methods to be performed on sqlite database such as create, update, delete, select etc.
Methods of SQLiteDatabase class
There are many methods in SQLiteDatabase class. Some of them are as follows:
Method Description
i. void execSQL(String sql)
-executes the sql query not select query.
ii. long insert(String table, String nullColumnHack, ContentValues values)
-inserts a record on the database. The table specifies the table name, nullColumnHack doesn’t allow completely null values. If second argument is null, android will store null values if values are empty. The third argument specifies the values to be stored.
iii. int update(String table, ContentValues values, String whereClause, String[] whereArgs) Â Â Â Â -updates a row.
iv. Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
-returns a cursor over the resultset.
Example of SQLite Database with few simple steps:
1. Create an Activity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.app.Activity; import android.database.Cursor; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyDatabase myDatabase = new MyDatabase(this); //Opening the connection myDatabase.openConnection(); //Inserting the data myDatabase.insertData(); //Fetching data from database and taking in cursor Cursor cursor = myDatabase.getData(); //Checking for data in cursor if (cursor != null) { while (cursor.moveToNext()) { Toast.makeText(this, "Name : " + cursor.getString(1) + " and Age : " + cursor.getString(1), Toast.LENGTH_LONG).show(); } } //Finally, closing the connection myDatabase.closeConnection(); } } |
2. Create a class and extend SQLiteOpenHelper class in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDatabase extends SQLiteOpenHelper { SQLiteDatabase db; public MyDatabase(Context context) { super(context, "demo_db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE demo(name TEXT , age TEXT )"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { //To Do } public void openConnection() { db = this.getWritableDatabase(); } public void closeConnection() { db.close(); } public void insertData() { ContentValues cv = new ContentValues(); cv.put("name", "Abhi"); cv.put("age", "25"); db.insert("demo", null, cv); } public Cursor getData() { return db.rawQuery("SELECT * FROM demo", null); } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments