Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ROWID in SQLite automatically set?

So, I have this application on Ionic that uses the SQLite ngCordova plugin for internal storage. On it, I create a table with the following command:

db.executeSql(
    "CREATE TABLE IF NOT EXISTS Scans_table (" +
      //"id         TEXT PRIMARY KEY  NOT NULL," +
      "name       TEXT              NOT NULL," +
      "comment    TEXT, " +
      "text       TEXT              NOT NULL, " +
      "format     TEXT              NOT NULL, " +
      "dateTaken  TEXT              NOT NULL, " +
      "imgSource  TEXT              NOT NULL)", ... );

According to this, if I don't have a column that is a primary key and an integer, a ROWID should be set as a unique identifier.

The problem is, when I query the table with a simple SELECT * ... I see all the rows that I set, and not the ROWID.

Is there a different way to set/check the ROWID?

like image 639
Sammy I. Avatar asked Aug 20 '16 04:08

Sammy I.


People also ask

Does Rowid change in SQLite?

From the official documentation: “Rowids can change at any time and without notice. If you need to depend on your rowid, make it an INTEGER PRIMARY KEY, then it is guaranteed not to change”.

Does primary key auto increment SQLite?

On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

What is auto increment SQLite?

SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table. We can auto increment a field value by using AUTOINCREMENT keyword when creating a table with specific column name to auto increment. The keyword AUTOINCREMENT can be used with INTEGER field only.


1 Answers

As shown in the documentation, your table does have an internal rowid column. However, if your table's column list does not include an alias for this column (i.e., an INTEGER PRIMARY KEY column), the rowid column is not included in SELECT *.

You can just add the column in the SELECT clause:

SELECT rowid, * FROM Scans_table ...

However, if you actually use the rowid, it is a better idea to have an explicit INTEGER PRIMARY KEY column. This not only documents the table structure better, but also prevents the rowid values from being changed by the VACUUM statement.

like image 182
CL. Avatar answered Oct 01 '22 13:10

CL.