samedi 3 janvier 2015

CONFLICT_NONE and CONFLICT_IGNORE still throw `android.database.sqlite.SQLiteConstraintException: column KEY_PHONE is not unique`

I want to a table on android:


phone table with primary key "id"



CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT UNIQUE,KEY_IS_BLOCKED BIT )


enter image description here


I have tried to add value to KEY_PHONE TEXT UNIQUE twice


I tried this



public long addItem(Phone phone) {
Log.d(Constants.LOGGER_TAG, "add saved-offer");
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();

// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
//values.put(KEY_ID, phone.id);
values.put(KEY_PHONE, phone.phone);
values.put(KEY_IS_BLOCKED, phone.isBlocked);

// 3. insert
long newRowId =
db.insertWithOnConflict(BLOCKED_PHONES_TABLE, KEY_ID,
values, SQLiteDatabase.CONFLICT_NONE);


and this



// 3. insert
long newRowId =
db.insertWithOnConflict(BLOCKED_PHONES_TABLE, KEY_ID,
values, SQLiteDatabase.CONFLICT_IGNORE);


I saw the tutorial, but didn't understand what should work to my case


(not add new row if already KEY_PHONE with same val exist in other row)


and yet i get this unique constraint error.



android.database.sqlite.SQLiteConstraintException: column KEY_PHONE is not unique (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:972)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1603)
at com.example.stopcall.app.dal.PhoneDal.addItem(PhoneDal.java:69)
at com.example.stopcall.app.fragments.ItemDetailFragment$2.onClick(ItemDetailFragment.java:84)
at android.view.View.performClick(View.java:4654)
at android.view.View$PerformClick.run(View.java:19438)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire