ContentProviderを使用してSQLiteDatabaseにデータを挿入していますが、挿入が失敗します。私が間違っていることを誰かが見てくれることを願っています。これが私のコードです:
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ブロックに入ります。最初の2行は実行されますが(newUriとgetContext()の割り当てなど)、コードはnewUriを返す代わりに、一番下までスキップしてnullを返します。誰かが理由を知っていますか?このスレッドを確認しましたが、私の場合には当てはまらないようです。前もって感謝します!