SQLiteConstraintException: error code 19: constraint failed — Android error

I have seen some other questions about this, but none of the answers really seemed to work for my code. I’m getting a ‘SQLiteConstraintException: error code 19: constraint failed’ error when I try to insert into my DB. Here is the code for the insert operation. db.insert returns a -1 right now, which leads to the “failed to insert” message.

Here is the the relevant Content Provider code:

  • Android Resource IDs
  • Programmatically Connect and Disconnect Android Device
  • How do I let a user upload files to internal storage?
  • Android change background image with fade in/out animation
  • Google Cloud Messaging for Android library vs. Google Play Services
  • Screen tracking support - Firebase 9.8
  • public static final String PROVIDER_NAME = "com.oliverapps.provider.DB";
    public static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER_NAME + "/tasks");
    public static final String _ID = "_id";
    public static final String CATEGORY = "category";
    public static final String STORE = "store";
    public static final String DESCRIPTION = "description";
    public static final String DISTANCE = "distance";
    public static final String CHECKED = "checked";
    private static final int KEY_CATEGORY = 1;
    private static final int KEY_STORE = 2;
    private static final int KEY_DESCRIPTION = 3;
    private static final int KEY_DISTANCE = 4;
    private static final int KEY_CHECKED = 5;
    private static final UriMatcher uriMatcher;
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "category", KEY_CATEGORY);
        uriMatcher.addURI(PROVIDER_NAME, "category/store", KEY_STORE);
        uriMatcher.addURI(PROVIDER_NAME, "category/store/description", KEY_DESCRIPTION);
        uriMatcher.addURI(PROVIDER_NAME, "category/store/description/distance", KEY_DISTANCE);
        uriMatcher.addURI(PROVIDER_NAME, "checked", KEY_CHECKED);
    //---for database use---
    private SQLiteDatabase tasksDB;
    private static final String DATABASE_NAME = "tasks";
    private static final String DATABASE_TABLE = "tasks" +
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE +
        " (" + _ID +  " integer primary key autoincrement, " +
        CATEGORY + " text not null, " + STORE + " text not null, " + DESCRIPTION + " text, " +
        DISTANCE + " text not null, " + CHECKED + " text not null);";
    private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        public void onCreate(SQLiteDatabase db){
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
            Log.w("Content provider database", "Upgrading database from version " +
                    oldVersion + " to " + newVersion + ", which will destroy all old data");
    public Uri insert(Uri uri, ContentValues values){
        //---add a new task---
        long rowID = tasksDB.insert(DATABASE_TABLE, "", values);
        //---if added successfully---
        if(rowID > 0){
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
            getContext().getContentResolver().notifyChange(_uri, null);
            return _uri;
        throw new SQLException("Failed to insert row into " + uri);
    public boolean onCreate(){
        Context context = getContext();
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        tasksDB = dbHelper.getWritableDatabase();
        return (tasksDB == null)? false:true;

    Here is the relevant code from the Android Manifest:

        <provider android:name="DBProvider"
            android:authorities="com.oliverapps.provider.DB" />

    And here is the relevant code that calls the insert method above:

    ContentValues values = new ContentValues();
                    values.put(DBProvider.CATEGORY, category);
                    values.put(DBProvider.STORE, store);
                    values.put(DBProvider.DESCRIPTION, description);
                    values.put(DBProvider.DISTANCE, distance);
                    Uri uri = getContentResolver().insert(DBProvider.CONTENT_URI, values);

    Any clue why this isn’t working? This is mostly code from an Android book I have, modified to my own database.

    Related posts:

    android device id confusion
    How to get all table names in android sqlite database?
    Pinterest like custom GridView
    IntentRecieverLeakedException, Are you missing a call to unregisterReceiver() ? in android
    How to set multiple tags to a button?
    Android sticky header RecyclerView/ Section Header RecyclerView
  • What causes a MotionEvent.ACTION_CANCEL in Android?
  • Get ActionBarSherlock actionBarSize in Android 2.x
  • Detect delete button in soft keyboard
  • android button selector
  • What is the difference between the states selected, checked and activated in Android?
  • How to Clear Recent Projects List in Android Studio?
  • One Solution collect form web for “SQLiteConstraintException: error code 19: constraint failed — Android error”

    The typical errors are following:

    • You’re trying to insert null values for columns which are constraint to be not null (so maybe missed to set values, maybe even because of wrong column constant definition).
    • You’re inserting same value for a column which is declared unique (typical primary key column)
    • You set values of wrong type for insert (e.g. column is integer but you set a string)

    I think you forgot to set CHECKED so it’s null and violates the not null constraint.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.