我需要读取(查询)数据库列的确切行。这是我的提供者的相关数据:
public class TravelOrderProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/travelorder");
private static final int URI_TRAVELORDER = 1;
private static final int URI_TRAVELORDER_ITEM = 2;
private static final UriMatcher mUriMatcher;
static {
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, "travelorder", URI_TRAVELORDER);
mUriMatcher.addURI(AUTHORITY, "travelorder/#", URI_TRAVELORDER_ITEM);
}
public class TravelOrder implements BaseColumns {
public static final String NAME = "name";
public static final String GROUP = "group";
public static final String ORDER = "order";
}
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
database = mDbHelper.getWritableDatabase();
int match = mUriMatcher.match(uri);
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
qBuilder.setTables(TravelOrderDatabaseHelper.TABLE_NAME);
switch (match){
case URI_TRAVELORDER:
//nothing
break;
case URI_TRAVELORDER_ITEM:
String id = uri.getPathSegments().get(1);
qBuilder.appendWhere(TravelOrder._ID + "=" + id);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
Cursor c = qBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
现在,这就是我在我的活动中尝试做的事情:
private static final String[] PROJECTION = {TravelOrder._ID, TravelOrder.NAME, TravelOrder.GROUP, TravelOrder.ORDER};
Cursor c = getContentResolver().query(TravelOrderProvider.CONTENT_URI, PROJECTION, null, null, null);
我的问题是:如何获得GROUP列的特定行?我现在应该在上面的最后一个代码行中完成,但是我尝试在selection
和selectionArgs
定义中定义列和行而没有结果。
答案 0 :(得分:0)
关于这个问题:
如何获得GROUP列的特定行?
关于如何在Android培训页面上"Read Information from a Database",有一个很好的解释示例。
引用:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_UPDATED,
...
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
因此,通过相应地更改selection
和selectionArgs
参数,您肯定可以获取数据库的GROUP列的特定行。
例如,假设您要获取与GROUP
列的特定值匹配的行。所以你应该像这样设置你的查询:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
/* The columns used for the SELECT statement. */
String[] projection = {
"ID",
"GROUP"
};
/*
* The columns used for the WHERE statement,
* they should be formatted as a prepared statement.
*/
String selection = "`ID` = ? AND `GROUP` = ?";
/*
* The arguments that will be replaced for each ?
* in the above statement.
*/
String[] selectionArgs = {
desiredId, desiredGroup
};
String sortOrder = "`ID` ASC";
Cursor c = db.query(
"MY_TABLE", // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);