0

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?

1 Answers1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • OK thanks. If that's the case, the next question should be: "Is contentvalues easier to understand, remember and code" since the existing examples seem to use it? OR is there no difference whatsover except for SQL being more reliable? I don't understand why the examples never used SQL. Perhaps someone else knows the answer? – Just Learning Apr 12 '22 at 05:08
  • Coding your own SQL is less reliable. The examples probably didn't encode SQL because it is easier to use the convenience methods. SQL, at least with Android, is typically seen when there are no convenience methods such as in the SQLiteOpenHeleper's overidden `onCreate` method that is frequently used to create the tables. Of course for insert the benefits of using the method are relatively high, especially the fact that it does more than just enact the INSERT statement. – MikeT Apr 12 '22 at 07:25
  • You have to elaborate. What do you mean by "because it is easier to use the convenience methods."? Easier to code, less code or perhaps the programmers are lazy? – Just Learning Apr 12 '22 at 08:14