SQLite的错误“列_id是不是唯一的”插入一个空表时,对 [英] SQLite error 'column _id is not unique' on when inserting into an empty table

查看:285
本文介绍了SQLite的错误“列_id是不是唯一的”插入一个空表时,对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个字段的表: _id 隐藏

当试图插入以下值:

  _id = 167从= 1311005879000到= 1311005879000隐藏= 0
 

在我的表,我得到了一个 SQLiteConstraintException 说明

 列_id不唯一(code 19)
 

要找到病因对于这个问题我想查询表的大小,发现它是0,所以我不知道在哪里,这是来自哪里。

也许是我没有正确理解错误?

编辑:一些code

 尝试{
    mDatabase.insertOrThrow(集团,空,
            mContentValues​​);
}赶上(的SQLException E){
    e.printStackTrace();
}
 

创建SQL:

  CREATE TABLE IF NOT EXISTS组(_id长PRIMARY KEY,隐藏INTEGER,从龙,龙
 

解决方案
  

列_id不唯一(code 19)

所以,你违反了唯一约束。这是理性的 SQLiteConstraintException 。你的 _id 列是最有可能的主键和主键都隐式分配唯一约束说两行都不具有相同的主键。

正在试图插入duplicit _id 已在数据库或 PK 分配给 NULL

  

我想查询表的大小,发现它是0,所以我没有   知道在哪里,这是来自哪里。

我觉得你的查询被打破了,因为你的异常说的一切,它可以不被拆毁,如果某个地方是没有问题的。

更新:

如果您未分配 NULL PK ,也表有0的记录可能问题就在这里:

  mDatabase.insertOrThrow(集团,空,mContentValues​​);
 

您分配 NULL 来ColumnHack为的第二个参数插入()方法,你不应该。 SQLite中,每一行都必须至少有一列指定。它需要一列,可以安全分配给 NULL

I have a table with 4 fields: _id, to, from, hidden

Upon trying to insert the following values:

_id=167 from=1311005879000 to=1311005879000 hidden=0

into my table, I got an SQLiteConstraintException stating that

'column _id is not unique (code 19)'

To find the cause for this problem I tried querying the size of the table and found it is 0, so I have no idea where this is coming from.

Maybe I didn't understand the error correctly?

Edit: some code!

try {
    mDatabase.insertOrThrow("groups", null,
            mContentValues);
} catch (SQLException e) {
    e.printStackTrace();
}

Creation SQL:

CREATE TABLE IF NOT EXISTS groups(_id LONG PRIMARY KEY,hidden INTEGER,from LONG,to LONG

解决方案

'column _id is not unique (code 19)'

So you are violating UNIQUE Constraint. This is reason of SQLiteConstraintException. Your _id column is most likely primary key and primary keys have implicitly assigned UNIQUE constraint that say no two rows can have same primary key.

You are trying to insert duplicit _id that already is in db or PK is assigned to NULL.

I tried querying the size of the table and found it is 0, so I have no idea where this is coming from.

I think your query was broken because your Exception says everything and it cannot be thrown if somewhere is not a problem.

Update:

If you are not assigned NULL to PK and also your table has 0 records probably problem is here:

mDatabase.insertOrThrow("groups", null, mContentValues);

You are assigned NULL to ColumnHack as second param of insert() method that you shouldn't. In SQLite, each row must have at least one column specified. It needs one column that can be safe assigned to NULL.

这篇关于SQLite的错误“列_id是不是唯一的”插入一个空表时,对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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