I have an android app with sqlite database. Sometimes, when I update a table, I get the following error:
error code 19: constraint failed android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
I donβt see which restriction can fail, because I do not have foreign keys and I do not insert a NULL value.
Table:
"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);";
Update Code:
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();
The exception is the string updateTempStatement.execute();.
The only limitation that I see is "NOT NULL", but the method Info.getlt()returns a primitive float, so it cannot be NULL.
Any other ideas?
source
share