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?
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”.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With