Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite data types

Tags:

java

sqlite

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?

like image 367
Manuel Selva Avatar asked Aug 02 '10 13:08

Manuel Selva


People also ask

What are data types in SQLite?

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.

Which one is not data type of SQLite?

Boolean Datatype SQLite does not have a separate Boolean storage class.

What is the difference between int and Integer in SQLite?

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.


3 Answers

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.

like image 156
Tangent 128 Avatar answered Nov 08 '22 00:11

Tangent 128


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.

like image 21
2 revs, 2 users 86% Avatar answered Nov 08 '22 01:11

2 revs, 2 users 86%


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.

like image 7
dan04 Avatar answered Nov 08 '22 00:11

dan04