如何在contentprovider中运行此查询?
SELECT * FROM sms_data WHERE number != (SELECT DISTINCT number from test) AND time > ?
答案 0 :(得分:2)
首先,查询可能是错误的。而不是number != (...)
您可能需要number NOT IN (...)
。
假设test
是您应用中的一个表而不是内容提供商,您可以分两步执行查询:
子查询SELECT DISTINCT number FROM test
。从结果中构建逗号分隔的字符串,例如'12345','23456','45678'
。
使用选择
进行内容提供商查询number NOT IN (<that comma-separated list>) AND time > ?
答案 1 :(得分:1)
对于子查询,您可以使用rawQuery()或SQLiteQueryBuilder。
答案 2 :(得分:0)
覆盖ContentProvider的onQuery方法,我设法将这一堆代码放在我身上。
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (URI_MATCHER.match(uri)) {
case TABLE1:
Cursor cursor = database.query("sms_data", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
case TABLE2:
Cursor c = database.query(true, "test", projection, selection, selectionArgs, null, null, sortOrder, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
case COMBINED:
String sql = "SELECT " + appendColumns(projection) +
" FROM sms_data WHERE number != IFNULL((SELECT DISTINCT number FROM test), 'abc') AND " + selection;
Cursor cur = database.rawQuery(sql, selectionArgs);
cur.setNotificationUri(getContext().getContentResolver(), uri);
return cur;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri + " Match = " + URI_MATCHER.match(uri));
}
}
希望这也适合你。