Skip to content Skip to sidebar Skip to footer

Error Inserting Android.database.sqlite.sqliteconstraintexception Error Code 19 Constraint Failed

I know there are so lot of threads about this problem but none of my own problem. I have a spinner connected in the database that displaying the foreign key of the specific table.

Solution 1:

Seems like some values you are inserting may be NULL for NOT NULL fields (ConsumerName in your case) or PK(_id in your case) already exists. Error code 19 means a table constraint (NOT NULL, UNIQUE, etc.) was violated during the operation (INSERT, etc.). Here are the list of SQLITE Error Codes

android.database.sqlite.SQLiteCursor@4144fa58 is junk value assigned to ConsumerName. So please do some workaround to get your proper FK for ConsumerName from cursor.

There is something wrong with you SimpleCursorAdapter. So please look on it. If you are getting correct values in spinner then please get selected value from spineer like this spinner.getSelectedItem().toString(); instead of spinner.getItemAtPosition(mSpinnerSpeciesId).toString().

Hope this helps you.

Solution 2:

Constraint failed usually indicates that you did something like pass a null value into a column that you declare as not null when you create your table.

So, Remove not null and place null while creating table.

Solution 3:

I got the same error and solved it by setting ID NULL in code. Since ID is PK SqlLite autoincrements its value, it does not need to be set.

publicvoidinsertConsumption(String meter_number, String current,
    String previous, String kWh, String date, String consumer_name) {

    ContentValues newCons = newContentValues();

    //newCons.put("_id", meter_number);
    newCons.put("Current", current);
    newCons.put("Previous", previous);
    newCons.put("kWh", kWh);
    newCons.put("Date", date);
    newCons.put("ConsumerName", consumer_name);

    open();

    database.insert("Consumptions", null, newCons);

    close();
}

Solution 4:

you declared the column "colAccountID" as PRIMARY KEY, so the SQLite db throw error while you trying to insert same id again and again.

Post a Comment for "Error Inserting Android.database.sqlite.sqliteconstraintexception Error Code 19 Constraint Failed"