If sqlite is typeless, so why we declare the datatype of the column ? what's the use of the declare datatype?
Why android SQLite can store double value(java 8 bytes) into float column
Some commentators say that SQLite is "weakly typed" and that other SQL databases are "strongly typed". We consider these terms to be inaccurate and even pejorative. We prefer to say that SQLite is "flexibly typed" and that other SQL database engines are "rigidly typed".
SQLite Affinity Type SQLite supports the concept of type affinity on columns. Any column can still store any type of data but the preferred storage class for a column is called its affinity.
A BLOB (large binary object) is an SQLite data type that stores large objects, typically large files such as images, music, videos, documents, pdf, etc. We need to convert our files and images into binary data (byte array in Python) to store it into SQLite database.
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.
The declared column data type is used as a hint (http://sqlite.org/faq.html#q3):
So, for example, if a column is of type INTEGER and you try to insert a string into that column, SQLite will attempt to convert the string into an integer. If it can, it inserts the integer instead. If not, it inserts the string. This feature is called type affinity.
By the way, SQLite is not typeless; it uses dynamic typing. This means that any value that you insert maintains its own type information, instead of inferring the type from the table declaration. http://sqlite.org/datatype3.html
It's used to determine the "affinity" of the column.
CREATE TABLE Example (
IntegerAffinity INTEGER,
RealAffinitity REAL,
NumericAffinity NUMERIC,
TextAffinity TEXT,
NoAffinity
);
Inserting some values into this table (with Python's sqlite3
module) gives):
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", [1234] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", ['1234'] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", [math.pi] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", [str(math.pi)] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", ['ABC'] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", [b'5678'] * 5)
>>> db.execute("INSERT INTO Example VALUES(?, ?, ?, ?, ?)", [b'\xAB\xCD\xEF'] * 5)
>>> list(db.execute("SELECT * FROM Example"))
[(1234, 1234.0, 1234, '1234', 1234),
(1234, 1234.0, 1234, '1234', '1234'),
(3.141592653589793, 3.141592653589793, 3.141592653589793, '3.14159265358979', 3.141592653589793),
(3.141592653589793, 3.141592653589793, 3.141592653589793, '3.141592653589793', '3.141592653589793'),
('ABC', 'ABC', 'ABC', 'ABC', 'ABC'),
(b'5678', b'5678', b'5678', b'5678', b'5678'),
(b'\xab\xcd\xef', b'\xab\xcd\xef', b'\xab\xcd\xef', b'\xab\xcd\xef', b'\xab\xcd\xef')]
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