2016-09-30 2 views
-1

어떻게이 예외를 해결할 수 있습니까? pleaz는 제가 그리드 뷰에 항목을 추가 할 수 있도록 도와 주지만 그리드 뷰의 마지막 항목을 클릭하면이 예외가 올바르게 삭제 된 항목을 표시하지만이 예외는 무엇이 던져 졌는지 나타냅니다. 내 코드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

문제 –

관련 문제