Android Accessing SQLite databases using the ContentValues class Inserting and updating rows in a SQLite database

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

First, you need to open your SQLite database, which can be done as follows:

SQLiteDatabase myDataBase; 
String mPath = dbhelper.DATABASE_PATH + dbhelper.DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READWRITE);

After opening the database, you can easily insert or update rows by using the ContentValues class. The following examples assume that a first name is given by str_edtfname and a last nameby str_edtlname. You also need to replace table_name by the name of your table that you want to modify.

Inserting data

ContentValues values = new ContentValues();
values.put("First_Name", str_edtfname);
values.put("Last_Name", str_edtlname);
myDataBase.insert("table_name", null, values);

Updating data

ContentValues values = new ContentValues();
values.put("First_Name", str_edtfname);
values.put("Last_Name", str_edtlname);
myDataBase.update("table_name", values, "id" + " = ?", new String[] {id});


Got any Android Question?