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:
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;
static{
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);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
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");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
@Override
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);
}
@Override
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.
The typical errors are following:
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).integer
but you set a string
)I think you forgot to set CHECKED
so it's null
and violates the not null
constraint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With