Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android sqlite constraint failed on update

Tags:

android

sqlite

I have an android app with a sqlite database. Sometimes when I update my table I get following error:

error code 19: constraint failed android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

I don't see which constraint can fail since I have no foreign keys and I'm not inserting a NULL value.

The 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);";     

The 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 on the line of updateTempStatement.execute(); .

The only constraint I see is the "NOT NULL" but the method Info.getlt() returns a float primitive, so it can't be NULL.

Any other ideas?

like image 646
Ran Avatar asked Aug 13 '26 08:08

Ran


1 Answers

1) One of important constraints of your table is primary key. This error occurs when you try to update primary key or insert duplicate primary key into your table.

2) try:

c.moveToFirst();
do {    
    //...
} while (c.moveToNext());

3) Do you pass parameters correctly to Info(...)?

4) Do you assign values correctly in Info(...)?

like image 79
Bobs Avatar answered Aug 14 '26 21:08

Bobs