'Android Studio: ContentValues vs Database.execSQL()

My question can be considered as a continuation of What is the difference between inserting data using ContentValues and Raw SQL in SQLite Android?

My question is more towards: Which is more common for programmers? Or is it a question of what kind of projects they are working on? I notice that all the examples I see in a google search uses contentvalues rather than SQL. Is there a reason for this? Perhaps SQL is no longer in fashion. Or is this due to the more recent Android studio?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s