How to avoid inserting the wrong data type into SQLite tables?

SQLite has this “function” , whereas even when creating a column of type INTEGERor REALit allows you to insert a row into it, even a row without numbers in it, for example, “a quick fox jumped over a lazy dog”.

How do you prevent such insertions in your projects?

I mean, when my code has an error that leads to this kind of insertions or updates, I want the program to throw an error, so I can debug it, and not just insert garbage into my database silently.

+4
source share
2 answers

, CHECK (. ).

   CREATE TABLE T (
       N   INTEGER CHECK(TYPEOF(N) = 'integer'),
       Str TEXT CHECK(TYPEOF(Str) = 'text'),
       Dt  DATETIME CHECK(JULIANDAY(Dt) IS NOT NULL)
   );
+6

(isNumeric, isString ..), ...

+1

All Articles