Search code examples
sqliteinsertprimary-keyrowid

SQLite: primary key and sqlitedatabase.insert


I have a table with an INTEGER PRIMARY KEY. That means, that my primary key becomes an alias for the rowid. However rowid is a long (according to the return value of SQLiteDatabase.insert) while my primary key is an integer. So what happens if I insert data at a time where all possible integer values are in use in that table? Since my primary key is an integer I would expect an error. But since my primary key refers to / is an alias for the rowid (which is a long -> still numbers available) SQLiteDatabase.insert could go ahead and insert my data, returning the rowid ... right? Or does SQLiteDatabase.insert restrict itself to the integer value range but still just returns a long every time? If so, wouldn't it make sense, to cast the return value of SQLiteDatabase.insert to int immediately? Because I could never get a "long-value" from that method anyway (due to my primary key integer)... or could I?


Solution

  • From: https://www.sqlite.org/datatype3.html

    INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value

    As you can see in SQLite the INTEGER data type is not what int is in Java,
    but it can store values up to 8 bytes just like the long type of Java.