Invalid Android Android Limit

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?

+3
source share
4 answers

1) One of the important limitations of your table is the primary key. This error occurs when you try to update the primary key or insert a duplicate of the primary key in the table.

2) try:

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

3) Info (...)?

4) Info (...)?

+1

-, ?

int hsuInd = c.getColumnIndex( "HSU" );

, HS.

-, ?

ContentValues values = new ContentValues();
values.put("LT", (Double) result[i].getLt());
int result = db.update("HIST", values, "_id = " + c.getInt("_id"), null);
+1

double check that result[i].getLt()it never really returns null

0
source

You should call updateTempStatement.execute();rebuild the loop.

0
source

All Articles