Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between "Text" and "String" datatype in SQLite

Tags:

sqlite

I have a SQLite database with my Android application. I have noticed I accidentally defined a table using a "String" datatype instead of "Text" datatype. Here is the code with string:

private final String LISTDATES_CREATE = "create table if not exists ListDates (_id integer primary key autoincrement, ListName string not null, UpdateDate string not null);"; 

This works. It has never thrown an error and I can store and retrieve data. However I can't find any reference to a "String" datatype in SQLite in the documentation or on the internet. Typically, all string type data is defined with "text" like so:

private final String LISTDATES_CREATE = "create table if not exists ListDates (_id integer primary key autoincrement, ListName text not null, UpdateDate text not null);"; 

So my question is, what is the difference between a field defined with a "string" datatype versus a "text" datatype? Is there a difference? If so, what are the consequences, if any, of using one or the other?

like image 940
Michael Stoner Avatar asked Aug 13 '12 16:08

Michael Stoner


People also ask

What is difference between text and varchar in SQLite?

More Details. TEXT has a fixed max size of 2¹⁶-1 = 65535 characters. VARCHAR has a variable max size M up to M = 2¹⁶-1 . So you cannot choose the size of TEXT but you can for a VARCHAR .

What are the 4 main data types in SQLite3?

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 datatype is used to store string in SQLite database?

TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

How long can text be in SQLite?

SQLite text and BLOB values are always variable length. The maximum size of a text or BLOB value is limited by a compile-time directive. The default limit is exactly one billion bytes, or slightly less than a full gigabyte.


1 Answers

The subtle thing to note here is that SQLite does not enforce the data type of values you put into columns. That means that you can put text into a numeric field, and so on.

To understand the difference between your two SQL statements, check out section 2.1 Determination Of Column Affinity, which maps the column types you provide to the storage classes SQLite uses.

In this case, the type string gets mapped to storage class NUMERIC via rule 5. Declaring the field as text in code would tell the DBMS to use the TEXT storage class. Again, since SQLite does not enforce the types of columns, your code will probably run fine when storing Strings as a NUMERIC column, as you note.

As an alternative example, you could define a column with type INTERESTING STUFF, and that would be mapped to the INTEGER storage class, via rule 1.

Overall, it's probably a good idea to just use text for your table definition.

like image 190
wsanville Avatar answered Sep 22 '22 20:09

wsanville