0
我正在使用ContentProvider插入數據到SQLiteDatabase中,插入失敗。我希望有人會看到我做錯了什麼。這裏是我的代碼:插入失敗與ContentProvider
public Uri insert(Uri uri, ContentValues values) {
int uriType = sURIMatcher.match(uri);
if (uriType != ALLDATA) {
throw new IllegalArgumentException("Invalid URI for insert");
}
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
try {
long newID = sqlDB.insertOrThrow(TABLE_BASEPATH,
null, values);
if (newID > 0) {
Uri newUri = ContentUris.withAppendedId(uri, newID);
getContext().getContentResolver().notifyChange(uri, null);
return newUri;
} else {
throw new SQLException("Failed to insert row into " + uri);
}
} catch (SQLiteConstraintException e) {
Log.i(DEBUG_TAG, "Ignoring constraint failure.");
}
return null;
}
MDB是被設置爲這樣一個全局變量:
private SQLData mDB;
SQLDATA是我的數據庫類的名稱。當我在調試器中關注這段代碼時,它會驗證newID是否大於0並進入if塊。前兩行執行(賦值newUri和getContext()等),但代碼跳到底部並返回null,而不是返回newUri。有誰知道爲什麼?我看過this thread,但它似乎不適用於我的情況。提前致謝!
其實它可能是很多事情,但這些是兩個我先檢查。同時檢查notifyChange是否觸發某種後續調用。 – Emile
你是對的,埃米爾。調用函數確實返回一個非空Uri。奇怪的。所以那不是我的問題。謝謝! – Melanie