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.

Author: Rachee Singh
Published? true -- FormatLanguage: W

Problem:

Adding values into a SQLite database.

Solution:

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

Discussion:

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));
tasks.add(t);

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

No records found.