当前位置: 动力学知识库 > 问答 > 编程问答 >

database - Android/SQLite. Can't add new value to column bacause logcat says the column not unique, but I defined it as unique?

问题描述:

Maybe here are some problem in creating the table, but I can't figure out.

android.database.sqlite.SQLiteConstraintException: column number is not unique (code 19)

String CREATE_SMSData_TABLE = "CREATE TABLE version3 ("

+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "

+ "number TEXT NOT NULL UNIQUE )";

db.execSQL(CREATE_SMSData_TABLE);

My insert operation:

public void addData(SMSData data) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(KEY_NUMBER, data.getNumber());

db.insert(TABLE_NAME, null, values);

db.close();

}

网友答案:

You are not setting a unique value in the "number" column. It is the same for each insert. You have marked the column as UNIQUE, and are attempting to insert the same value multiple times.

Either guarantee a unique value for the "number" column, in the ContentValues, for each insert, or remove the UNIQUE constraint from the table.

Edited to add additional case:

... or, I suppose, if the value really is supposed to be unique, catch the Exception and take appropriate action.

分享给朋友:
您可能感兴趣的文章:
随机阅读: