Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - SQLite error 'column _id is not unique' on when inserting into an empty table

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

'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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...