mercredi 5 août 2015

SQLite Constraint Exception

I am writing an android application, to do some sqllite processing. This is my table creation statements.

    TABLE_CREATE_REVOKED_TOKENS = "CREATE TABLE IF NOT EXISTS " + TABLE_REVOKED_TOKENS + " (";
    TABLE_CREATE_REVOKED_TOKENS += "resource varchar(20) NOT NULL,";
    TABLE_CREATE_REVOKED_TOKENS += "base int(11) NOT NULL,";
    TABLE_CREATE_REVOKED_TOKENS += "delta int(11) NOT NULL,";
    TABLE_CREATE_REVOKED_TOKENS += "data blob NOT NULL,";
    TABLE_CREATE_REVOKED_TOKENS += "PRIMARY KEY (resource, base, delta));";

I have the following data in the table.

resource    base delta  data
"Safe 1"    "1"  "0"    "�<]FSb>�/���Z���d�J�"����E�"
"Safe 1"    "1"  "1"    "�������.���V�����X ��wE���"

When I try to inset a new row into the table. I get the following exception.

Error inserting data=[B@41a99f98 base=1 resource=Safe 1 delta=2
android.database.sqlite.SQLiteConstraintException: columns resource, base, delta, data are not unique (code 19)

This is code to insert the statements.

private int insertNewRevocationList(RevocationList rl) {
    String TABLE = TABLE_REVOKED_TOKENS;
    ContentValues initialValues = new ContentValues();
    initialValues.put("resource", new String(rl.getResource()));
    initialValues.put("base", rl.getBase());
    initialValues.put("delta", rl.getDelta());
    initialValues.put("data", rl.getData());

    Log.d(TAG, "create new entry for:");
    Log.d(TAG, "resource: " + new String(rl.getResource()) + "\tbase: " + rl.getBase() + "\tdelta: " + rl.getDelta());
    return (int) getDb().insert(TABLE, null, initialValues);
}

Aucun commentaire:

Enregistrer un commentaire