2016-09-30 127 views
-1

我怎麼能解決這個異常懇求幫我我添加項目到網格視圖,但當我點擊到最後一項在網格視圖此異常出現該項目正確刪除,但此異常拋出什麼是錯在我的代碼android SQLiteConstraintException UNIQUE約束失敗

public class MyDataBase extends SQLiteOpenHelper { 
private static final int DATABASE_VERSION = 1; 
// Database Name 
private static final String DATABASE_NAME = "contactsManager"; 
// Contacts table name 
private static final String TABLE_CONTACTS = "contacts"; 
private static final String TABLE_DELETED = "deleted"; 
// Contacts Table Columns names 
private static final String KEY_ID = "id"; 
final String KEY_NAME = "name"; 
private static final String KEY_dID = "did"; 
final String KEY_dNAME = "dname"; 
public MyDataBase(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT UNIQUE" 
      + ")"; 
    String CREATE_DELETED_TABLE = "CREATE TABLE " + TABLE_DELETED + "(" 
      + KEY_dID + " INTEGER PRIMARY KEY," + KEY_dNAME + " TEXT UNIQUE" 
      + ")"; 

    db.execSQL(CREATE_CONTACTS_TABLE); 
    db.execSQL(CREATE_DELETED_TABLE); 
} 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 
public void AddnewContact(photos todo)// this method to add new contact 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, todo.getImage()); 
    db.insert(TABLE_CONTACTS, null, values); 
    db.close(); 
} 
public void AddnewContactToDeletedTable(photos todo)// this method to add new contact 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_dNAME, todo.getImage()); 
    db.insert(TABLE_DELETED, null, values); 
    db.close(); 
} 
public ArrayList<photos> getallcontacts() { 
    ArrayList<photos> contactList = new ArrayList<photos>(); 
    String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 
    if (cursor.moveToFirst()) { 
     do { 
      photos contact = new photos(); 
      contact.setId(Integer.parseInt(cursor.getString(0))); 
      contact.setImage(cursor.getString(1)); 
      // Adding contact to list 
      contactList.add(contact); 
     } while (cursor.moveToNext()); 
    } 
    // return contact list 
    return contactList; 
} 
public void deleteContact(photos contact) { 
    int id = contact.getId(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_CONTACTS, KEY_ID 
      + " = " + id, null); 
    db.close(); 
} 

}

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: contacts.name (code 2067) 
                     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) 
                     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:788) 
                     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:1471) 
                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) 
                     at com.example.yasser.ahlysc.MyDataBase.AddnewContact(MyDataBase.java:55) 
                     at com.example.yasser.ahlysc.Funny$1.onResponse(Funny.java:59) 
                     at com.example.yasser.ahlysc.Funny$1.onResponse(Funny.java:48) 
                     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 
                     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
                     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:211) 
                     at android.app.ActivityThread.main(ActivityThread.java:5389) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:372) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

回答

2

我覺得你的插入,這就是爲什麼拋出這個錯誤,替代的解決方案是可以更換的查詢相同的值。

試試下面的代碼:

public void AddnewContact(photos todo)// this method to add new contact 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, todo.getImage()); 
    db.replace(TABLE_CONTACTS, null, values); // change this line to your code 
    db.close(); 
} 

替代查詢可以處理已存在的數據...

+0

沒有解決問題 –

相關問題