如何在 SQLite 3.31.1 中启用严格模式?

互联网 行业动态 更新时间:2024-06-13 00:19:06

Ano*_*ard 5

根据文档,您添加STRICT到表声明的末尾:

$ sqlite3
SQLite version 3.37.0 2021-11-27 14:13:22
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE yay ( col1 TEXT, col2 INT ) STRICT;
sqlite> INSERT INTO yay ( col1, col2 ) VALUES ("this works", "this is the wrong type");
Error: stepping, cannot store TEXT value in INT column yay.col2 (19)

需要注意的是,这是 SQLite 版本 3.37.0 的新功能。在任何以前版本的 SQLite 上,表创建都会失败。并且任何以前的版本都将无法打开使用此标志创建的数据库文件,并出现“格式错误的数据库模式”错误,除非您PRAGMA writable_schema=ON在打开数据库之前指定了编译指示。

您可以在旧版本的 SQLite 上使用类似的CHECK 约束概念:

SQLite version 3.31.0 2020-01-22 18:38:59
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE yay ( col1 TEXT CHECK (typeof(Col1) = 'text'), col2 INT CHECK (typeof(Col2) = 'integer'));
sqlite> INSERT INTO yay ( col1, col2 ) VALUES ("this works", "this is the wrong type");
Error: CHECK constraint failed: yay

更多推荐

模式,如何在,SQLite

本文发布于:2023-04-20 20:47:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/5b68caff1ff218a09dda774dda151d44.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   如何在   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!