SQLiteConstraintException:错误代码19:约束失败-Android错误 [英] SQLiteConstraintException: error code 19: constraint failed -- Android error
问题描述
我还看到了其他一些问题,但是这些答案似乎都不适合我的代码.尝试插入数据库时,出现"SQLiteConstraintException:错误代码19:约束失败"错误.这是插入操作的代码. db.insert现在返回-1,这将导致插入失败"消息.
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;
}
以下是Android清单中的相关代码:
Here is the relevant code from the Android Manifest:
<provider android:name="DBProvider"
android:authorities="com.oliverapps.provider.DB" />
这是调用上面的insert方法的相关代码:
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);
有什么线索为什么这行不通?这主要是我拥有的一本Android书中的代码,已修改为我自己的数据库.
Any clue why this isn't working? This is mostly code from an Android book I have, modified to my own database.
推荐答案
典型错误如下:
- 您要为约束不是
null
的列插入null
值(因此可能会丢失设置值,甚至可能是因为错误的列常量定义). - 您要为声明为唯一的列(典型的主键列)插入相同的值
- 您为插入设置了错误的类型值(例如,列为
integer
,但您设置了string
)
- You're trying to insert
null
values for columns which are constraint to be notnull
(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 astring
)
我认为您忘记设置CHECKED
,所以它是null
,并且违反了not null
约束.
I think you forgot to set CHECKED
so it's null
and violates the not null
constraint.
这篇关于SQLiteConstraintException:错误代码19:约束失败-Android错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!