6

Dear Stack Overflow Community,

I have a little problem here with starting my first attempt to Database-Design.

I try to make some Kind of a tabular and try to check out the Basic functions; writing with Python3 and SQLite3.

My question is quite simple, I have a few lines:

cur.execute('CREATE TABLE IF NOT EXISTS test(a INTEGER, argument DATE)')

Now I can just write:

cur.execute('insert into test (a, argument) values (?,?)', "bla", "blabla")

Without getting an error message; shouldn't this command be automatically forbidden by SQL when you define the allowed datatypes in the CREATE TABLE statement?

Full code is here: http://codepad.org/kf4gZB7m

1
  • Welcome to SO, Update your question with the code in the question Commented May 31, 2017 at 17:53

3 Answers 3

5

This is a feature, not a bug. SQLite uses dynamic typing. See https://sqlite.org/faq.html#q3

Sign up to request clarification or add additional context in comments.

5 Comments

Okay thank you for the answer! Is this Feature connected to some Performance losses or higher memory consumption? And if yes, is there a way to disable this feature?
Don't know about memory consumption but there is certainly no way to make SQLite more type-strict. You'd need a different SQL implementation for that.
SQLite often uses less memory and is faster than other DBs, but comparing embedded and client/servers DBs does not really make sense. You can add manual checks if you really need them.
It's a pretty terrible feature. There should at least be a way to turn on proper type checking. :-/
@Timmmm there is now: sqlite.org/stricttables.html
2

This feature is Type Affinity:

SQL database engines that use rigid typing will usually try to automatically convert values to the appropriate datatype. The important idea here is that the type is recommended, not required.

Docs: https://sqlite.org/datatype3.html#affinity

Comments

1

As of version 3.37.0 (2021-11-27), SQLite supports a strict typing mode to fix its most infamous feature. It must be enabled per-table with the keyword STRICT.

> CREATE TABLE t(a INTEGER PRIMARY KEY, b TEXT) STRICT;
> INSERT INTO t VALUES (2, "foo");
> INSERT INTO t VALUES ("foo", "bar");
Runtime error: datatype mismatch (20)

For better or for worse, SQLite will still try to silently coerce values to match the column type even in strict mode:

INSERT INTO t VALUES ("1", 2);   -- A-okay!

However, if no lossless coercion is possible, like using "foo" as an integer above, an error is raised.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.