SQLiteConstraintException: column id is not unique (code 19) 更新行时的异常
SQLiteConstraintException: column id is not unique (code 19) exception when updating row
我的数据库中有一个 table,当添加新记录时,我正在使用 id.If 检查记录是否存在,我想更新记录,如果不存在添加新记录。但是我得到了例外:
2006-2259/? E/SQLiteDatabase﹕ Error inserting id=080333 phone=080333 total_owed=15050.0 total_debts=0 name=Alison Jones
android.database.sqlite.SQLiteConstraintException: column id is not unique (code 19)
这是检查记录是否存在的方法:
public boolean checkIfExists(String tableName, String value) {
SQLiteDatabase database = this.getReadableDatabase();
String Query = "Select * from " + tableName + " where " + DEBTOR_ID + " = " + value;
Cursor cursor = database.rawQuery(Query, null);
if(cursor.getCount() <= 0){
cursor.close();
return false;
}
cursor.close();
return true;
}
这里正在使用它:
debtor.setDebtorName(params[0]);
debtor.setPhoneNumber(params[1]);
debtor.setTotalAmountOwed(Double.parseDouble(params[6]));
debtor.setTotalDebts(0);
if (databaseHandler.checkIfExists(DebtDatabaseHandler.DEBTOR_TABLE_NAME, params[1])) {
Log.d("NewDebtFragment", "Record already exist");
databaseHandler.updateRecord(2, debtor.getTotalAmountOwed() + 50000);
} else {
debtorId = databaseHandler.addDebtor(debtor);
}
由于这是一个冲突问题,显然不检查现有记录 working.I 不是因为代码看起来不错 (?)
这就是我创建 table 的方式:
String CREATE_DEBTOR_TABLE = "CREATE TABLE " + DEBTOR_TABLE_NAME + " ( " +
DEBTOR_ID + " TEXT PRIMARY KEY, " +
DEBTOR_NAME + " TEXT, " +
PHONE_NUMBER + " TEXT, "+
TOTAL_DEBTS + " INTEGER, " +
TOTAL_OWED + " INTEGER) ";
和updateRecord
方法:
public void updateRecord(int newTotalDebt, double newTotalOwed) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TOTAL_DEBTS, newTotalDebt);
contentValues.put(TOTAL_OWED, newTotalOwed);
db.update(DEBTOR_TABLE_NAME, contentValues, DEBTOR_ID, null);
}
用以下内容替换您的查询
String Query = "Select * from " + tableName + " where " + DEBTOR_ID + " = '" + value+"'";
因为选择 value
是字符串,所以必须用单引号括起来。
更新:
无需编写一大堆代码来检查记录是否退出,如果退出然后更新该记录,您可以用简单的一行替换所有这些东西。
database.insertWithOnConflict(table,null,values,SQLiteDatabase.CONFLICT_REPLACE);
它将在内部处理异常,CONFLICT_REPLACE
它指定当发生 UNIQUE 约束违规时,在插入或更新当前行之前删除导致约束违规的预先存在的行。
我的数据库中有一个 table,当添加新记录时,我正在使用 id.If 检查记录是否存在,我想更新记录,如果不存在添加新记录。但是我得到了例外:
2006-2259/? E/SQLiteDatabase﹕ Error inserting id=080333 phone=080333 total_owed=15050.0 total_debts=0 name=Alison Jones android.database.sqlite.SQLiteConstraintException: column id is not unique (code 19)
这是检查记录是否存在的方法:
public boolean checkIfExists(String tableName, String value) {
SQLiteDatabase database = this.getReadableDatabase();
String Query = "Select * from " + tableName + " where " + DEBTOR_ID + " = " + value;
Cursor cursor = database.rawQuery(Query, null);
if(cursor.getCount() <= 0){
cursor.close();
return false;
}
cursor.close();
return true;
}
这里正在使用它:
debtor.setDebtorName(params[0]);
debtor.setPhoneNumber(params[1]);
debtor.setTotalAmountOwed(Double.parseDouble(params[6]));
debtor.setTotalDebts(0);
if (databaseHandler.checkIfExists(DebtDatabaseHandler.DEBTOR_TABLE_NAME, params[1])) {
Log.d("NewDebtFragment", "Record already exist");
databaseHandler.updateRecord(2, debtor.getTotalAmountOwed() + 50000);
} else {
debtorId = databaseHandler.addDebtor(debtor);
}
由于这是一个冲突问题,显然不检查现有记录 working.I 不是因为代码看起来不错 (?)
这就是我创建 table 的方式:
String CREATE_DEBTOR_TABLE = "CREATE TABLE " + DEBTOR_TABLE_NAME + " ( " +
DEBTOR_ID + " TEXT PRIMARY KEY, " +
DEBTOR_NAME + " TEXT, " +
PHONE_NUMBER + " TEXT, "+
TOTAL_DEBTS + " INTEGER, " +
TOTAL_OWED + " INTEGER) ";
和updateRecord
方法:
public void updateRecord(int newTotalDebt, double newTotalOwed) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TOTAL_DEBTS, newTotalDebt);
contentValues.put(TOTAL_OWED, newTotalOwed);
db.update(DEBTOR_TABLE_NAME, contentValues, DEBTOR_ID, null);
}
用以下内容替换您的查询
String Query = "Select * from " + tableName + " where " + DEBTOR_ID + " = '" + value+"'";
因为选择 value
是字符串,所以必须用单引号括起来。
更新:
无需编写一大堆代码来检查记录是否退出,如果退出然后更新该记录,您可以用简单的一行替换所有这些东西。
database.insertWithOnConflict(table,null,values,SQLiteDatabase.CONFLICT_REPLACE);
它将在内部处理异常,CONFLICT_REPLACE
它指定当发生 UNIQUE 约束违规时,在插入或更新当前行之前删除导致约束违规的预先存在的行。