且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLite的错误“列_id是不是唯一的”插入一个空表时,对

更新时间:2023-12-01 13:54:58

  

列_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.