ContentValues are used in combination with the convenience (simple to use) methods, such as the SQLiteDatabase insert
method that reliably builds the appropriate SQL (including passing values via parameters).
They correctly enclose such values.
They cater for many scenarios are very convenient and also reliable. They may also do additional work that a single execSQL will not do, that is return a value.
For the insert
convenience method the value returned, is a long (Long in Kotlin) and is the rowid of the inserted row or -1 if the row was not inserted but the conflict was trapped (insert
includes OR IGNORE
so unique, null and check constraints do not result in an error condition).
- rowid is a normally hidden value that is generated by SQLite as is unique, with the exception of (relatively uncommon) WITHOUT ROWID tables all tables have a rowid.
- frequently the rowid is aliased and is often then referred to as the id.
- Using INTEGER PRIMARY KEY (with or without AUTOINCREMENT) as part of the column definition (PRIMARY KEY can be coded at the table level). INTEGER must be INTEGER, INT will not alias the rowid.
- The rowid can be up to twice as fast as access via other indexes.
- so the rowid or an alias thereof, is a very useful value to know, especially if it is generated as opposed to set specifically.
The coding is probably tidier.
Perhaps SQL is no longer in fashion. Or is this due to the more recent Android studio?
SQL is still used, it's just that it is written on your behalf by the convenience methods. It certainly does not hurt to have a good understanding of SQL.