2011-10-21 95 views
1

根據android documentation它具有完整的sqlite支持。這是否意味着我可以使用core functions作爲projection一部分查詢,例如:Android SQLite核心功能

final ContentResolver cr = getContentResolver();  
final String [] projection = new String []{ 
    ..., 
    ..., 
    "replace(...) as replacedCol" 
}; 

Cursor c = cr.query(uri, projection, null, null, null); 

這個片段拋出IllegalArgumentException無效的列替換(...))。 那麼有沒有辦法在sqlite查詢中使用核心功能?

編輯錯誤很可能是由ContentProvider引起的,它檢查允許的列,所以看起來使用帶有內容解析器的函數是不可能的。

+0

是你的ContentProvider嗎? – Selvin

+0

所以這是不可能的...但...假設你使用列表視圖/微調你可以建立你的自定義適配器,並在設置查看前替換此字符串(在getView或bindView) – Selvin

+0

@Selvin不,它是ContactsContract提供商 – Vladimir

回答

1

投影在提供查詢的SQL查詢<>列

通常的ContentProvider包含

static final HashMap<String, String> map = new HashMap<String, String>(); 
static { 
    map.put("NameInProjection", "RealDBColumn AS NameInProjection"); 
    map.put("replacedCol", "replace(...) as replacedCol"); 
} 

和ContentProvider的查詢方法,我們使用它像

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 
    builder.setTables(table); 
    builder.setProjectionMap(map); 
    return builder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); 
} 

所以SQLiteQueryBuilder投影翻譯成使用地圖進行真實查詢