1

我想使用内容提供者删除前“n”行。

这是我的代码:

    private void deleteOldItems(int number) {
    String where = HistoryTable.COLUMN_ID + "<=?";
    getContentResolver()
            .delete(Uri.parse("content://com.ipiit.client.contentprovider.HistoryContentProvider/histories"),
                    where, new String[] {String.valueOf(number)});
    }

我工作但只有一次。如何始终删除第一行?

4

2 回答 2

5

如果这是你的“自己的” ContentProvider,即你确定表名和类似的东西,你总是可以像这样做一个相当丑陋的子选择:

private void deleteOldItems(int number) {
    // You should probably sort the subselect on something
    // suitable indicating its age. The COLUMN_ID should do.
    String where = HistoryTable.COLUMN_ID + " IN (SELECT " + HistoryTable.COLUMN_ID + " FROM " + 
        HistoryTable.TABLE_NAME + " ORDER BY " + HistoryTable.COLUMN_ID + " LIMIT ?)";
    getContentResolver()
        .delete(Uri.parse("content://com.ipiit.client.contentprovider.HistoryContentProvider/histories"),
                where, new String[] {String.valueOf(number)});
}

另一方面,如果它是您自己的ContentProvider.. 只需在将其传递给 #delete(..) 时添加对向 URI 添加“限制”参数的支持,而不是诉诸像这样的丑陋黑客。

于 2012-11-15T12:58:42.907 回答
1

尝试这样的事情contentprovider

count = DBConnection.delete("TableName", "_id" + "=" +arg1, arg2);
于 2012-11-15T12:55:24.150 回答