SQLite has this "feature" whereas even when you create a column of type INTEGER
or REAL
, it allows you to insert a string into it, even a string without numbers in it, like "the quick fox jumped over the lazy dog".
How do you prevent this kind of insertions to happen in your projects?
I mean, when my code has an error that leads to that kind of insertions or updates, I want the program to give an error, so I can debug it, not simply insert garbage in my database silently.
SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB.
For this latter group, SQLite supports a strict typing mode, as of version 3.37. 0 (2021-11-27), that is enabled separately for each table.
insert or ignore ... will insert the row(s) and ignore rows which violation any constraint (other than foreign key constraints).
You can implement this using the CHECK
constraint (see previous answer here). This would look like
CREATE TABLE T (
N INTEGER CHECK(TYPEOF(N) = 'integer'),
Str TEXT CHECK(TYPEOF(Str) = 'text'),
Dt DATETIME CHECK(JULIANDAY(Dt) IS NOT NULL)
);
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