defining a “VARIANT” column type in SQLite?

浪子不回头ぞ 提交于 2021-02-08 06:54:19

问题


Is there a way to define a column type in SQLite that holds any datatype that SQLite supports?

I have unsigned chars, signedchars, unsigned ints, signed ints, UTF-8 chars and blobs that I want to populate in a single column.

Reference to any literature is also appreciated.


回答1:


Just don't put a type in the column declaration, so it has NONE affinity

create table my_table(my_col);

SQLite has a unique dynamic typing system. It has per-value typing, but if you specify a column type, SQLite will determine a type affinity (TEXT, NUMERIC, INTEGER, REAL, NONE). It then attempts to coerce each value to that affinity.

The actual supported types are NULL, INTEGER, REAL, TEXT, BLOB. See Datatypes in SQLite Version 3 for more information.




回答2:


Variant is the default in sqlite, the reverse is harder. It is difficult to define a column that can only tolerate numbers and will throw an exception when you want to insert "this is text".

You can assert that a column can only store integers with a check constraint:

CHECK(typeof(x)='integer')


来源:https://stackoverflow.com/questions/3731212/defining-a-variant-column-type-in-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!