gpt4 book ai didi

SQLITE 日期未检查类型

转载 作者:IT王子 更新时间:2023-10-29 06:28:58 27 4
gpt4 key购买 nike

我刚刚测试了 sqlite 并创建了一个表。

$sqlite3 plop.db

sqlite> CREATE TABLE t (d DATE);

sqlite> INSERT INTO t (d) VALUES ('Hello');

sqlite> PRAGMA table_info(t);
0|a|DATE|0||0

sqlite> SELECT * FROM t;
Hello

那么,为什么当我尝试将 CHAR(5) 插入 DATE 时没有出现错误?

最佳答案

来自 Sqlite 的 Frequently Asked Questions

SQLite lets me insert a string into a database column of type integer!

This is a feature, not a bug. SQLite uses dynamic typing. It does not enforce data type constraints. Any data can be inserted into any column. You can put arbitrary length strings into integer columns, floating point numbers in boolean columns, or dates in character columns. The datatype you assign to a column in the CREATE TABLE command does not restrict what data can be put into that column. Every column is able to hold an arbitrary length string. (There is one exception: Columns of type INTEGER PRIMARY KEY may only hold a 64-bit signed integer. An error will result if you try to put anything other than an integer into an INTEGER PRIMARY KEY column.)

But SQLite does use the declared type of a column as a hint that you prefer values in that format. So, for example, if a column is of type INTEGER and you try to insert a string into that column, SQLite will attempt to convert the string into an integer. If it can, it inserts the integer instead. If not, it inserts the string. This feature is called type affinity.

关于SQLITE 日期未检查类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com