我有一个包含以下实体的 SQLite
表:
[Table ("employee")]
public class Employees
{
[Column ("employee_id")]
[PrimaryKey]
public int EmployeeId { get; set; }
[Column ("username")]
public string UserName { get; set; }
[Column ("password")]
public string Password { get; set; }
[Column ("first_name")]
public string FirstName { get; set; }
[Column ("last_name")]
public string LastName { get; set; }
[Column ("is_wah_allowed")] //Newly added Column
public bool IsWAHAllowed { get; set; }
}
当应用程序启动时,我正在创建一个表。
conn.CreateTable<Employees>();
我想在 SQLite
中添加默认值设置为 0 - false 的新 bool 列,因为它将 bool 存储为 0 或 1。
我尝试了以下方法:
案例1:
[Column ("is_wah_allowed")] //Newly added Column
输出:这在 SQLite 的数据库浏览器中显示 Null
情况2:
[Column ("is_wah_allowed"), Default (false)] //Newly added Column
输出:这会在 conn.CreateTable() 处引发异常; - 无法从 bool 转换为 int。
情况3:
[Column ("is_wah_allowed"), Default (value : 0)] //Newly added Column
输出:这会在 SQLite 的数据库浏览器中显示 False 值,而不是 0。 当服务器返回数据时,我保存/更新它们,然后在更新的行中将它们显示为 0 或 1,因为服务器将其处理为整数值。
我想知道:
案例1:为什么显示Null而不是0默认值?
案例2:为什么会抛出异常?
情况3:为什么显示False而不是0?
提前致谢!!
最佳答案
SQLite 中不存在boolean
。相反,您可以将 boolean
值作为 SQLite 中的 int 值存储在数据库中。您可以使用的值是 0 表示 false,1 表示 true。当您从数据库中选择 int 时。根据 0 和 1 你可以纠正你的 if
else
block 。
编辑 1
bool 数据类型 SQLite 没有单独的 bool 存储类。相反, bool 值存储为整数 0(假)和 1(真)。
欲了解更多信息,请访问 this
关于SQLite 默认值为 bool 列,未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596868/