Android SQLite Query
I got the following 2 tables in my app's database
Table1:
FTSProfile
-----------
suggest_intent_data_id
suggest_shortcut_id
CELL_ID
NAME
_id
Table2:
FTSCell
-----------
suggest_intent_data_id
KEY_LAC
suggest_shortcut_id
_id
KEY_MCC
KEY_CID
KEY_MCC
And I want to perform a left join on the tables, with KEY_CID = KEY_CELL_ID. Every thing seems fine until i want to get the FTSCell._id and cast it as _ID in the resultant table. I used the SQLiteQueryBuilder to help building the query:
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS_VIRTUAL_TABLE_PROFILE+" LEFT JOIN "+FTS_VIRTUAL_TABLE_CELL+" ON "+FTS_VIRTUAL_TABLE_CELL+"."+DataCellQuery.KEY_CID+"="+FTS_VIRTUAL_TABLE_PROFILE+"."+DataProfileQuery.KEY_CELL_ID);
builder.setProjectionMap(mColumnMap_CombinedTable);
Cursor cursor = builder.query(mDatabaseOpenHelper.getWritableDatabase(), columns, selection, selectionArgs, null, null, null);
if (cursor == null) return null;
else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
and here is my HashMap being used:
private static HashMap<String, String> buildColumnMap_CombinedTable() {
HashMap<String,String> map = new HashMap<S开发者_运维百科tring,String>();
for (String key: DataCellQuery.KEYS) map.put(key, key);
map.put(BaseColumns._ID, "rowid AS " + FTS_VIRTUAL_TABLE_CELL+ "."+BaseColumns._ID);
map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " + SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
return map;
}
The query is perfect if I do not get the _id from the Cell table, but I need that in my code. How can I get rid of that??? The following error message is what I get...
05-19 15:56:05.054: ERROR/AndroidRuntime(7772): Caused by: android.database.sqlite.SQLiteException: near ".": syntax error: , while compiling: SELECT rowid AS FTSCell._id, KEY_MCC, KEY_MNC, KEY_CID, KEY_LAC FROM FTSProfile LEFT JOIN FTSCell ON FTSCell.KEY_CID=FTSProfile.CELL_ID WHERE (NAME="PX9RKTPYW4")
The syntax is:
select table.field as name, ...
not name as table.field
.
精彩评论