166k views
1 vote
Which method of the SQLiteDatabase class can you use to add a row to a table?

User Eric Broda
by
7.7k points

1 Answer

6 votes

Final answer:

To add a row to a table in SQLite using Android's SQLiteDatabase class, use the 'insert()' method alongside a ContentValues object that includes the new row's column values.

Step-by-step explanation:

To add a row to a table in an SQLite database using the Android SQLiteDatabase class, you would typically use the insert() method. This method allows you to specify the table you're inserting into, a null column hack (usually not used, so null can be passed), and a ContentValues object that contains the values for the new row. The ContentValues object works like a map that pairs column names with new column values.

Here's an example of how you might use insert() to add a new row:

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("column_name", "value");
// ...add more column-value pairs
long newRowId = db.insert("table_name", null, values);
if (newRowId == -1) {
// Handle error, insertion failed
} else {
// Row was inserted successfully
}

User Hariharan Gandhi
by
8.5k points