我的sqlite数据库中有2个表,所以现在我正在尝试为数据库创建内容提供程序。我到目前为止看到的所有例子只显示了2个表的CRUD代码,但没有显示checkColumns。我试过这个
private void checkColumns(String[] projection) {
if (Constants.COLUMNS != null) {
HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection));
HashSet<String> availableColumns = new HashSet<String>(Arrays.asList(Constants.COLUMNS));
if (!availableColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException("Unknown columns in projection");
}
}
if (Constants.pCOLUMNS != null) {
HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection));
HashSet<String> availableColumns = new HashSet<String>(Arrays.asList(Constants.pCOLUMNS));
if (!availableColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException("Unknown columns in projection");
}
}
}
然后对于查询,我做了这个
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
checkColumns(Constants.COLUMNS);
checkColumns(Constants.pCOLUMNS);
queryBuilder.setTables(Constants.ITEM_TABLE);
queryBuilder.setTables(Constants.PANTRY_TABLE);
int type = URI_MATCHER.match(uri);
switch (type) {
case ITEMS:
break;
case ITEM:
queryBuilder.appendWhere(Constants.COLUMN_iID + " = " + uri.getLastPathSegment());
break;
case sPANTRY:
break;
case PANTRY:
queryBuilder.appendWhere(Constants.COLUMN_pID + " = " + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
来自Constants.java的
public static final String[] COLUMNS = {
Constants.COLUMN_iID,
Constants.COLUMN_ITEM,
Constants.COLUMN_iQUANTITY,
Constants.COLUMN_iUNIT,
Constants.COLUMN_PRICE,
Constants.COLUMN_STORE
};
public static final String[] pCOLUMNS = {
Constants.COLUMN_pID,
Constants.COLUMN_PANTRY,
Constants.COLUMN_pQUANTITY,
Constants.COLUMN_pUNIT,
Constants.COLUMN_EXPIRY_DATE
};
}
但我收到此错误
Caused by: java.lang.IllegalArgumentException: Unknown columns in projection
我尝试在checkColumns中使用switch case,如此
private void checkColumns(String[] projection) {
switch(projection) {
case Constants.COLUMNS:
HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection));
HashSet<String> availableColumns = new HashSet<String>(Arrays.asList(Constants.COLUMNS));
if (!availableColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException("Unknown columns in projection");
}
break;
case Constants.pCOLUMNS:
HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection));
HashSet<String> availableColumns = new HashSet<String>(Arrays.asList(Constants.pCOLUMNS));
if (!availableColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException("Unknown columns in projection");
}
break;
}
}
但我需要不断表达&#39;在两个常数上。
答案 0 :(得分:0)
你得到“需要常量表达式”错误,因为checkColumns(String[] projection)
方法采用字符串数组,并且switch语句不能使用字符串数组作为“switch”变量。
第一种方法似乎没问题。您得到java.lang.IllegalArgumentException
可能是因为requestedColumns
和availableColumns
中存在不匹配(例如,可用列中不存在请求的列)。
假设逻辑是检查所有投影列是否在items和pantry列中,那么可以将Constants.COLUMNS和Constants.pCOLUMNS添加到一个数组中(比如“totalColumns”),然后进行检查:< / p>
private void checkColumns(String[] projection) {
if (projection != null) {
String[] totalColumns = ArrayUtils.addAll(Constants.COLUMNS, Constants.pCOLUMNS);
if (!totalColumns.containsAll(requestedColumns)) {
throw new IllegalArgumentException("Unknown columns in projection");
}
} else {
throw new IllegalArgumentException("Invalid column projection");
}
}
(请注意,您需要导入ArrayUtils,或者如果您不想导入它,您可以编写自己的...,无论如何,您明白了。)