SQLITE日期没有类型检查 [英] SQLITE date no type checked

查看:133
本文介绍了SQLITE日期没有类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  $ sqlite3 plop.db 

sqlite>创建表t(d DATE);

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

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

sqlite> SELECT * FROM t;
您好

所以,为什么当我尝试插入一个CHAR(5 )到DATE?

解决方案

从Sqlite的常见问题


SQLite允许我在一个数据库列中插入一个字符串整数!



这是一个功能,而不是一个bug。 SQLite使用动态类型。它不
强制数据类型约束。任何数据都可以插入任何
列。您可以将任意长度的字符串放入整数列,
布尔列中的浮点数或字符
列中的日期。您分配给CREATE TABLE
命令中的列的数据类型不会限制可以将该数据放入该列。每个
列可以容纳任意长度的字符串。 (有一个
异常:类型为INTEGER PRIMARY KEY的列可能只能保存64位
有符号整数,如果您尝试将任何其他
放入整数,则会导致错误INTEGER PRIMARY KEY列。)



但是,SQLite使用声明的列类型作为一个提示,您
更喜欢该格式的值。因此,例如,如果列为
,则键入INTEGER,并尝试在该列中插入一个字符串,SQLite
将尝试将该字符串转换为整数。如果可以,它
插入整数。如果没有,它会插入字符串。这个
功能称为类型关联。



I just tested sqlite and created a table.

$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

So, why there are no errors when I tryed to insert a CHAR(5) into a DATE?

解决方案

From Sqlite's 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日期没有类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆