Why is the datatype not working in ipython-sql?

51 Views Asked by At

When creating a database table in jupyter, we specify restrictions on data types in the columns of the table, but for some reason we can still add other data types. For example, the st_gr column should contain only numbers, but nothing will stop us from adding a line (code below) Why? How to fix?

%%sql sqlite://
CREATE TABLE students(
    st_id INTEGER PRIMARY KEY AUTOINCREMENT,
    fname VARCHAR(15) NOT NULL,
    lname VARCHAR(15) NOT NULL,
    st_gr NUMERIC
)

%%sql sqlite://
    INSERT INTO students (fname, lname, st_gr) VALUES('Barack', 'Obama', 'text not num')
2

There are 2 best solutions below

0
PChemGuy On BEST ANSWER

SQLite uses dynamic type system. Declared column type only indicates the preferred value type, unless the table is strict.

0
DinoCoderSaurus On

If you are using sqlite version > 3.37.0, look into using STRICT tables.

If not, perhaps check constraints? software verification? a different database?