布尔数据类型接受的字符串值,整数值

布尔数据类型接受的字符串值,整数值

问题描述:

表有布尔列,布尔应该只接受值为0或1,但它接受int值以及字符串值。

Table having boolean column ,boolean should only accept value 0 or 1 but it is accepting int value as well as string value.

这是表模式 -

CREATE TABLE maintable_slave ( din INTEGER, strip_no integer, strip_status boolean);

这是查询,通过它我更新列 -

And here is query through which i am updating column-

update maintable_slave set strip_status = 5 where din = 1;

update maintable_slave set strip_status = 'add' where din = 1;

在这里strip_status布尔列接受字符串和整数值。

here strip_status boolean column accepting string and integer value.

这是一个经常被问到的问题 - 这是字面上的的SQLite的常见问题解答

This is a frequently asked question—it's literally in SQLite's FAQ:

(3)的SQLite让我插入一个字符串到整数类型的数据库列!

这是一个特点,不是一个错误。 SQLite的使用动态类型。它不强制数据类型约束。任何数据可被插入到任何列。你可以把任意长度的字符串到整数列,漂浮在布尔列点编号或日期在字符列。 分配给一列在CREATE TABLE命令的数据类型并不限制什么样的数据可以被放入该列。每一列是能够容纳任意长度的字符串。 (有一个例外:类型 INTEGER PRIMARY KEY的列可能只保留64位有符号整数。将导致一个错误,如果你试图将不是整数以外的任何其他成 INTEGER PRIMARY KEY 列。)

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.)

但SQLite的确实使用声明的类型的列与您美元,该格式p $ PFER值的提示。因此,举例来说,如果一个列的类型INTEGER和您尝试插入一个字符串进入该列,SQLite的将尝试将字符串转换为整数。如果可以,它插入的整数代替。如果不是,它插入字符串。此功能称为型亲和力

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.