我有一个带有 sqlite 数据库的 android 应用程序。有时当我更新我的表时,我会收到以下错误:
error code 19: constraint failed android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
我没有看到哪个约束会失败,因为我没有外键并且我没有插入 NULL 值。
桌子:
"CREATE TABLE HIST (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"CL TEXT UNIQUE, " +
"ACOUNT INTEGER DEFAULT 0, " +
"HS INTEGER DEFAULT 0," +
"EX INTEGER DEFAULT 0," +
"LU INTEGER NOT NULL DEFAULT 0," +
"LT REAL NOT NULL DEFAULT 0);";
更新代码:
SQLiteStatement updateTempStatement = db.compileStatement("UPDATE HIST " +
" SET LT=? WHERE _id=?");
Cursor c = null;
c = db.rawQuery(QUERY_SQL, new String[] { hs?"1":"0" });
Info[] result = null;
if (c.getCount() > 0) {
result = new Info[c.getCount()];
int i = 0;
int idInd = c.getColumnIndex("_id");
int cInd = c.getColumnIndex("CL");
int hsuInd = c.getColumnIndex("HSU");
int ltInd = c.getColumnIndex("LT");
db.beginTransaction();
try {
while (c.moveToNext()) {
result[i] = new Info(c.getString(cInd),
c.getFloat(hsuInd),
c.getFloat(ltInd));
updateTempStatement.bindDouble(1, result[i].getLt());
updateTempStatement.bindLong(2, c.getLong(idInd));
updateTempStatement.execute();
i = i + 1;
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
}
c.close();
updateTempStatement.close();
例外是在updateTempStatement.execute();
.
我看到的唯一约束是,"NOT NULL"
但是该方法Info.getlt()
返回一个浮点原语,所以它不能为 NULL。
还有其他想法吗?