I am inserting data into my tables and i have to insert a value that can either be "Overdue", "Returned", or "Burrowed"
我正在向表中插入数据,并且我必须插入一个可以是“过期”、“返回”或“挖洞”的值。
Status string check(Status is "Overdue" OR "Borrowed" OR "Returned") NOT NULL,
状态字符串检查(状态为“逾期”或“借入”或“已归还”)非空,
i try to insert
我试着插入
select * from Books;
insert into Books(Book_ID, Book_Name, Author, Genre, Rating, Status)
values("200001", "Harry Potter", "J.K Rowling", "Fantasy", 5, "Returned");
but then i get the error message that
(Error while executing SQL query on database 'database': CHECK constraint failed: Status is "Overdue" OR "Borrowed" OR "Returned")
但随后我收到错误消息(在数据库‘DATABASE’上执行SQL查询时出错:检查约束失败:状态为“已过期”或“已借用”或“已返回”)
are they not the same?
它们不是一样的吗?
更多回答
Why do you use double quotes for usual strings?
为什么对常见的字符串使用双引号?
优秀答案推荐
The correction by @Mureinik is likely correct, but it is also worth pointing out what your current check constraint is actually doing:
@Mureinik的更正可能是正确的,但也值得指出您当前的CHECK约束实际在做什么:
Status string check(Status is "Overdue" OR "Borrowed" OR "Returned") NOT NULL
The IS
operator will behave as =
here, according to the SQLite documentation:
根据SQLite文档,is操作符的行为将为=HERE:
The IS and IS NOT operators work like = and != except when one or both of the operands are NULL.
In addition, string literals should evaluate to false, hence:
此外,字符串的计算结果应为FALSE,因此:
Status string check(Status = "Overdue" OR false OR false) NOT NULL
which is the same as:
这与以下内容相同:
Status string check(Status = "Overdue") NOT NULL
In other words, your current check constraint will only admit "Overdue" as a possible status. As @Mureinik suggested, use this version:
换句话说,您当前的检查约束将只承认“已过期”作为可能的状态。正如@Mureinik建议的那样,使用以下版本:
Status string CHECK (Status IN ('Overdue', 'Borrowed', 'Returned'));
This isn't the right usage of the is
operator. I think you meant to use the in
operator:
这不是is运算符的正确用法。我想您是想使用In运算符:
Status IN ("Overdue", "Borrowed", "Returned")
If you were using in frame work, declare model level enum for status and add default values
如果您在Frame Work中使用,请声明模型级别枚举作为状态并添加默认值
更多回答
yes that fixed it, thank you
是的,修好了,谢谢
Green checkmark to the left if possible?
如果可能,请在左侧打上绿色复选标记?
i have done that but the problem persists
我已经这样做了,但问题仍然存在
The question wasn't looking for an alternative way of implementing that which has already been implemented but a solution to a check constraint problem.
问题不是寻找实现已经实现的方法的替代方法,而是寻找检查约束问题的解决方案。
我是一名优秀的程序员,十分优秀!