I am using SQLite in a Java application through Zentus. In this context I need to save and query Java long
values in my database. Coming from other RDBMS I created the table as following to store long values:
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue LONG)
This solution produces the excepted behavior but after reading the SQLite documentation on data types I understood that my LONG
type has the same effect than using TEXT
=> longValue
is stored as text.
I then decided to change this to INTEGER
(which length is variable and can store up to 64 bit integers which is the length of Java long) in order to have cleaner code and may be to save some disk space and to increase performances because my longValues are inserted and queried as long
.
After comparing the performances and the size of the created databases I am not able to see any difference between:
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue LONG)
and
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue INTEGER)
Any comments, experiences or feelings on the subject?
SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB. APIs that return database values as an object will only ever return one of these four types.
Boolean Datatype SQLite does not have a separate Boolean storage class.
However, the dynamic typing in SQLite allows it to do things which are not possible in traditional rigidly typed databases. So in MS Sql Server (for example), an "int" == "integer" == 4 bytes/32 bits. In contrast, a SqlLite "integer" can hold whatever you put into it: from a 1-byte char to an 8-byte long long.
In SQLite, data types are per-value, not per-column. So when you insert integers, they're stored as integers regardless of the column type.
SQLite chooses automatically the right size. From http://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.
SQLite uses dynamic types and is schema free.
After looking at performances and size of the created databases I am not able to see any difference between:
There isn't any difference. INTEGER has integer affinity and LONG has numeric affinity. And, http://www.sqlite.org/datatype3.html says:
A column that uses INTEGER affinity behaves the same as a column with NUMERIC affinity. The difference between INTEGER and NUMERIC affinity is only evident in a CAST expression.
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