【问题标题】:Why sqlite autoincrement int cant be null ?为什么 sqlite autoincrement int 不能为 null ?
【发布时间】:2014-03-18 04:32:21
【问题描述】:

根据Docs可以将null传递给AUTOINCREMENT列实现自增功能,但是,当列为int时,显然一个不能传递null给它。

如何解决?

dbhelper.addEntry(new Entry(intCantBeNull, "StringOK"));

【问题讨论】:

  • 如果您不向该字段传递值,则 SQLite 将使用自动递增的整数值。或者你可以使用 Integer 类,它接受空值
  • 这是我的第一个猜测,但不,它没有。 @user2340612
  • 请发布您的插入代码,以便我们更好地帮助您省略自动增量列值。
  • 如果您使用 SQLiteDatabase insert 方法并且在 ContentValues 参数中您没有为 auto-incremented column 指定值,那么您将得到一个 auto-incremented value
  • 可以(通常)为自动递增字段传递NULL,而sqlite3会做它应该做的事情。您似乎遇到的问题是 API 问题(我以外的其他人必须帮助解决)。 NULL 不是 Java 的 null,除非 API 说它是。

标签: java android sqlite auto-increment


【解决方案1】:

您可以尝试如下插入数据,

假设您有两个字段 Id(即自动增量)和名称(TEXT)

Insert into yourTableName(name) values ( "StackOverflow" );

实际上,您将NULL 传递到由SQLite 自动处理的AUTOINCREMENT 字段。

【讨论】:

  • 我知道这种 SQL-y 方式。我想使用我的 dbhelper @user3322955
  • @Shervin,那个页面已经写了the NULL is automatically converted into an integer which is one greater than the largest value of that column over all other rows in the table, or 1 if the table is empty.
  • @Shervin,对不起,我没接你。
  • 该页面上所说的不是我所经历的。省略“自动增量”字段会出错。
猜你喜欢
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2013-09-22
  • 2012-10-17
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多