【发布时间】:2017-05-17 03:07:00
【问题描述】:
我正在尝试使用内容提供程序从 SQLite 数据库中仅删除一行,但我的代码删除了所有行。这是我正在做的事情:
我正在传递这个 uri:
content://appfactory.app.dehleezcafe/category/1
其中“类别”是表名,“1”是要删除的记录。
这是我在某处调用的删除代码:
// Delete category
this.getContentResolver().delete(
contentURI,
null,
null
);
contentURI 等于传递的 uri。
ContentProvider 类中的 delete 方法发生了什么:
rowsDeleted = db.delete(CategoryEntry.TABLE_NAME, selection, selectionArgs);
这将删除所有行而不是第“1”行??这不是我想要的。我很感激任何建议。
这是我的合同:
public static final String CONTENT_AUTHORITY = "appfactory.app.dehleezcafe";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_CATEGORY = "category";
public static final String PATH_ITEM = "item";
Category BaseColumns 类:
public static final class CategoryEntry implements BaseColumns {
public static final String TABLE_NAME = "category";
public static final String COLUMN_CATEGORY_NAME = "name";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_CATEGORY).build();
public static final String CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_CATEGORY;
public static Uri buildCategoryUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
Item BaseColumns 类
public static final class ItemEntry implements BaseColumns {
public static final String TABLE_NAME = "item";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_PRICE = "price";
public static final String COLUMN_PHOTO = "photo";
// Column with the foreign key into the category table.
public static final String COLUMN_CATEGORY_KEY = "category_id";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_ITEM).build();
public static final String CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_ITEM;
public static Uri buildItemUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
创建语句:
// Category table create statement
final String SQL_CREATE_CATEGORY_TABLE = "CREATE TABLE " + CategoryEntry.TABLE_NAME + " (" +
CategoryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
CategoryEntry.COLUMN_CATEGORY_NAME + " TEXT NOT NULL, " +
" )";
// Item table create statement
final String SQL_CREATE_ITEM_TABLE = "CREATE TABLE " + ItemEntry.TABLE_NAME + " (" +
ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
// the ID of the category entry associated with this item data
ItemEntry.COLUMN_CATEGORY_KEY + " INTEGER, " +
ItemEntry.COLUMN_NAME + " TEXT NOT NULL, " +
ItemEntry.COLUMN_DESCRIPTION + " TEXT NOT NULL, " +
ItemEntry.COLUMN_PRICE + " TEXT NOT NULL, " +
ItemEntry.COLUMN_PHOTO + " BLOB, " +
// Set up the category column as a foreign key to category table.
" FOREIGN KEY (" + ItemEntry.COLUMN_CATEGORY_KEY + ") REFERENCES " +
CategoryEntry.TABLE_NAME + " (" + CategoryEntry._ID + ") " +
" )";
// Constructor
public MenuSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
内容提供者中的删除方法:
// Delete Method
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Student: Start by getting a writable database
final SQLiteDatabase db = menuDbHelper.getWritableDatabase();
// Student: Use the uriMatcher to match the WEATHER and LOCATION URI's we are going to
// handle. If it doesn't match these, throw an UnsupportedOperationException.
final int matchVal = uriMatcher.match(uri);
int rowsDeleted = 0;
// This makes delete all rows return the number of rows deleted
if(selection == null)
selection = "1";
switch (matchVal) {
case ITEM:
rowsDeleted = db.delete(
MenuContract.ItemEntry.TABLE_NAME, selection, selectionArgs);
break;
case CATEGORY:
rowsDeleted = db.delete(
MenuContract.CategoryEntry.TABLE_NAME, selection, selectionArgs);
break;
case ITEM_WITH_CATEGORY:
rowsDeleted = db.delete(CategoryEntry.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new UnsupportedOperationException("Unknown Uri: " + uri);
}
// A null value deletes all rows.
if(rowsDeleted != 0 ) {
getContext().getContentResolver().notifyChange(uri, null);
}
// Return the actual rows deleted
return rowsDeleted;
}
我可以识别案例,但仅删除一行没有成功。
编辑
奇怪的是,我可以使用以下代码从“item”表中删除特定列而没有问题:
// Delete items of a category
String[] selctionArg = {String.valueOf(id)};
this.getContentResolver().delete(
ItemEntry.CONTENT_URI,
ItemEntry.COLUMN_CATEGORY_KEY+"=?",
selctionArg
);
对“类别”表使用相同的代码不会删除任何内容,并且使用之前的代码会删除所有行而不是特定行
【问题讨论】:
标签: android sqlite android-contentprovider