我怎样才能解决这个异常pleaz帮助我添加项目到网格视图但是当我点击网格视图中的最后一项时,此异常会显示项目被正确删除但是此异常抛出了我的代码中的错误< / p>
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)
答案 0 :(得分:2)
我认为您插入相同的值就是抛出此错误的原因,替代解决方案是您可以替换查询。
尝试以下代码:
Horse&
替换查询可以处理已有的数据......