SQLiteConstraintException:错误代码19:约束失败 - Android错误

时间:2011-10-08 22:33:53

标签: android android-contentprovider

我已经看到了其他一些问题,但是没有一个答案真正适用于我的代码。当我尝试插入我的数据库时,我收到'SQLiteConstraintException:错误代码19:约束失败'错误。这是插入操作的代码。 db.insert现在返回-1,这导致“无法插入”消息。

以下是相关的内容提供商代码:

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;
}

以下是Android Manifest的相关代码:

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

以下是调用上面插入方法的相关代码:

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);

任何线索为什么这不起作用?这主要是来自我的Android书籍的代码,修改为我自己的数据库。

1 个答案:

答案 0 :(得分:42)

典型错误如下:

  • 您正在尝试为约束不是null的列插入null值(因此可能会错过设置值,甚至可能因为错误的列常量定义)。
  • 您为声明为唯一的列(典型的主键列)
  • 插入相同的值
  • 您为插入设置了错误类型的值(例如,列为integer,但您设置了string

我认为您忘了设置CHECKED所以它是null并违反not null约束。