Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ContentValues: Are values misordered at insert?

Obviously I didn't understand the concept of ContentValues in Android. How is it guaranteed that an insert statement like database.insert(TABLE, null, contentValues); obtains all values of contentValues in the right order, since ContentValues is a set?

Let's say TABLE has 3 string columns. If I write the following...

contentValues.put("col1", valueCol1);
contentValues.put("col2", valueCol2);
contentValues.put("col3", valueCol3);

...it is not guaranteed that database.insert inserts the values in this order, or did I understand something wrong?

What are the benefits in using database.insert instead of db.execSQL at all (except better View handling in Android)?

like image 995
Bevor Avatar asked Jan 25 '26 21:01

Bevor


1 Answers

since ContentValues is a set?

ContentValues much more closely resembles a Map. It is key-value store, keyed by strings.

it is not guaranteed that database.insert inserts the values in this order, or did I understand something wrong?

An INSERT will insert those values simultaneously, the way INSERT statements have worked for the past couple of decades in any SQL-compliant database.

To use your example, if we were to insert() that ContentValues into a table named foo, the result would be equivalent to the following:

db.execSQL("INSERT INTO foo (col3, col1, col2) VALUES (?, ?, ?)", args);

where args is a String[] consisting of valueCol3, valueCol1, and valueCol2, respectively.

What are the benefits in using database.insert instead of db.execSQL at all

It involves a bit less typing, and you do not have to convert your values to a String array.

(except better View handling in Android)?

IMHO, insert() on SQLiteDatabase does not offer "better View handling" than does execSQL() on SQLiteDatabase, though perhaps you are thinking of something that I am missing.

like image 141
CommonsWare Avatar answered Jan 28 '26 14:01

CommonsWare