sql ContentProvider中的多表查询
参考:http://www.eoeandroid.com/thread-550720-1-1.html
/** * 多表查询 * @param companyId 根据公司id查询 * @return */ public ArrayList<MenuItemModel> queryMultiTable(String companyId) { //projection代表查询结果返回的列 String[] projection = { "menutreeitems." + MenuTreeItemsModelIds.ITEMID, "menutreeitems." + MenuTreeItemsModelIds.COMPANYID, "menutreeitems." + MenuTreeItemsModelIds.DESCRIPTIONS, "menutreeitems." + MenuTreeItemsModelIds.FILEID, "menutreeitems." + MenuTreeItemsModelIds.ISDELETED, "menutreeitems." + MenuTreeItemsModelIds.ITEMTYPE, "menutreeitems." + MenuTreeItemsModelIds.NAME, "menutreeitems." + MenuTreeItemsModelIds.PARENTITEMID, "menutreeitems." + MenuTreeItemsModelIds.ROWVERSION, "filedetails." + BaseColumns._ID, "filedetails." + FileDetailsIds.EXTENSION, "filedetails." + FileDetailsIds.FILE_NAME}; //在这里多表查询的URI是重点CONTENT_URI(当前表的URI)
//"filedetails"(代表另一张表的表名)
// content://athority/table1/table2 Uri uri = Uri.withAppendedPath(CONTENT_URI, "filedetails");
Cursor c = null; ContentProviderClient client = mContentResolver.acquireContentProviderClient(MuliTableContentProvider.AUTHORITY); try { c = client.query(uri, projection, MenuItemModelIds.COMPANYID+"="+companyId, null, null); } catch (RemoteException e) { e.printStackTrace(); } //创建ArrayList用来存放数据 ArrayList<MenuItemModel> dataList = new ArrayList<MenuItemModel>(); //将指针移动到第一行 c.moveToFirst(); while(!c.isAfterLast()){ String itemId = c.getString(c.getColumnIndex(BaseColumns._ID)); String parentItemId = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.PARENTITEMID)); String name = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.NAME)); String descriptions = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.DESCRIPTIONS)); String fileId = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.FILEID)); String itemType = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.ITEMTYPE)); // String companyId = c.getString(c.getColumnIndex(RoomAndTableTreeItemsModelIds.NAME)); String rowVersion = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.ROWVERSION)); String isDeleted = c.getString(c.getColumnIndex(MenuTreeItemsModelIds.ISDELETED)); String fileName = c.getString(c.getColumnIndex(FileDetailsIds.FILE_NAME)); MenuItemModel model = new MenuItemModel(); model.setItemId(itemId); model.setParentItemId(parentItemId); model.setName(name); model.setDescriptions(descriptions); model.setFileId(fileId); model.setItemType(itemType); model.setRowVersion(rowVersion); model.setIsDeleted(isDeleted); model.setFileName(fileName); dataList.add(model); c.moveToNext(); } c.close(); return dataList; }