Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Inserting values into a SQLite database.

In Chapter: Data Persistence
Author: Rachee Singh ('racheesingh')
Published? true
FormatLanguage: WikiFormat


Adding values into a SQLite database.


Using insert() method and passing an object of ContentValues class.


ContentValues provides something like a key-value pair, so NAME would be the key and 'Mangoes' would be the value. This would insert a row in the database with value 'Mangoes' in it.

ContentValues values = new ContentValues();
values.put(NAME, "Mangoes");

After creating the values we insert it into the table using the insert method. SQLite returns the ID for that row in the database.

Long id = (database.insert(TABLE_NAME, null, values));

'id' is the ID for the row that we inserted into the database.