码迷,mamicode.com
首页 > 数据库 > 详细

sql ContentProvider中的多表查询

时间:2015-03-02 18:31:13      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

参考: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; }

 

sql ContentProvider中的多表查询

标签:

原文地址:http://www.cnblogs.com/wjw334/p/4309406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!