SQLiteDatabase

List<AppInfo> appList = mAppInfoDBOptHelper.getMyInstalledAppInfos();

    /**
     * 获取已安装的应用列表
     */
    public List<AppInfo> getMyInstalledAppInfos() {
        List<AppInfo> installedAppInfoList = new ArrayList<AppInfo>();
        List<AppInfoEntity> appInfoList = new AppInfoDao(mContext).query(
                AppStoreDataBaseHelper.APP_INFO_COLUMN_INSTALL_STATUS,
                Constant.INSTALLED_STATUS);
        if (appInfoList != null && appInfoList.size() > 0) {
            for (AppInfoEntity installAppInfo : appInfoList) {
                String appId = installAppInfo.getAppId();
                AppInfo appInfoTemp = new AppInfo();
                if (appId != null) {
                    detailAppInfo(appInfoTemp, installAppInfo);
                }
                installedAppInfoList.add(appInfoTemp);
            }
        }
        return installedAppInfoList;
    }
    
    
    
    /**
     * 指定数据表列名和列的值查询应用信息记录
     *
     * @param whereColumn
     *            表中的列名称
     * @param whereColumnValue
     *            表中的列值
     * @return
     */

    public synchronized List<AppInfoEntity> query(String whereColumn,String whereColumnValue) {
        List<AppInfoEntity> ret = null;
        if ((null == whereColumn) || (null == whereColumnValue)) {
            ILog.dPrint(TAG, "query parameter ERROR. " + " whereColumn="
                    + whereColumn + " whereColumn=" + whereColumnValue);
            return ret;
        }
        SQLiteDatabase mDatabase = null;
        Cursor cursor = null;
        try {
            mDatabase = dbOpenHelper.getReadableDatabase();
//            if(!mDatabase.isOpen()){
//                mDatabase.reopenReadWrite();
//            }
            while(mDatabase.isDbLockedByOtherThreads()){
                Thread.sleep(10);
            }
            if (mDatabase != null) {
                cursor = mDatabase.rawQuery("select * from "
                        + AppStoreDataBaseHelper.APP_INFO_TABLE_NAME
                        + " where " + whereColumn + "=?" +" order by "+AppStoreDataBaseHelper.APP_INFO_COLUMN_ID,
                        new String[] { whereColumnValue});
                if (null == cursor) {
                    ILog.dPrint(TAG, "query() cursor is null");
                } else {

                    int count = cursor.getCount();
                    if (count > 0) {
                        ret = new ArrayList<AppInfoEntity>();
                        for (int i = 0; i < count; i++) {
                            cursor.moveToNext();
                            AppInfoEntity retObj = cursor2Entity(cursor);
                            ret.add(retObj);
                        }
                    } else {
                        ILog.dPrint(TAG, "Can not find record in database by "
                                + whereColumnValue + " and appInfoEntity");
                    }
                }

            } else {
                ILog.dPrint(TAG,
                        "You need open this database before get a record");
            }
        } catch (IllegalStateException e) {
            ILog.ePrint(TAG, "IllegalStateException ");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
            }
            if (null != mDatabase) {
                dbOpenHelper.close();
            }

        }

        return ret;
    }
    
    private AppInfoEntity cursor2Entity(Cursor cursor) {
        AppInfoEntity ret = null;
        try {
            ret = new AppInfoEntity();
            String[] fieldNames = new String[] { "id", "appId", "packageName",
                    "typeName", "appName", "version", "versionCode", "size",
                    "price", "language", "minSystem", "developer",
                    "publishTime", "commentSize", "downloadTimes",
                    "averageScore", "description", "installStatus",
                    "updateStatus", "downloadStatus", "downloadOrder",
                    "signCertId" };
            String[] tableCols = new String[] {
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_ID,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_APP_ID,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_PACKAGE_NAME,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_TYPE_NAME,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_APP_NAME,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_VERSION,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_VERSION_CODE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_SIZE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_PRICE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_LANGUAGE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_MIN_SYSTEM,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_DEVELOPER,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_PUBLISH_TIME,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_COMMENT_SIZE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_DOWNLOAD_TIMES,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_AVERAGE_SCORE,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_DESCRIPTION,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_INSTALL_STATUS,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_UPDATE_STATUS,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_DOWNLOAD_STATUS,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_DOWNLOAD_TASK_ORDER,
                    AppStoreDataBaseHelper.APP_INFO_COLUMN_SIGNCERTID };
            int[] propertyTypes = new int[] { AppStoreDataBaseHelper.LONG_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.LONG_TYPE,
                    AppStoreDataBaseHelper.DOUBLE_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.LONG_TYPE,
                    AppStoreDataBaseHelper.LONG_TYPE,
                    AppStoreDataBaseHelper.DOUBLE_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE,
                    AppStoreDataBaseHelper.LONG_TYPE,
                    AppStoreDataBaseHelper.STRING_TYPE };
            AppStoreDataBaseHelper.cursor2Entity(ret, cursor,
                    AppInfoEntity.class, fieldNames, tableCols, propertyTypes);
        } catch (EntityConvertException e) {
            e.printStackTrace();
            ret = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
    
    
    /**
     * @param entity
     *            需要设置值的对象
     * @param cursor
     *            数据库游标对象
     * @param clazz
     *            需要设置值的对象的类型类对象
     * @param fieldNames
     *            对象中的属性名数组
     * @param tableCols
     *            数据库表中的字段数组
     * @param propertyTypes
     *            对象中属性的类型数组 此三个参数必须,且数组长度必须一致
     * @throws EntityConvertException
     */
    public static void cursor2Entity(Object entity, Cursor cursor, Class clazz,
            String[] fieldNames, String[] tableCols, int[] propertyTypes)
            throws EntityConvertException {

        if ((tableCols == null) || (fieldNames == null)
                || (propertyTypes == null)) {
            throw new EntityConvertException("must param is null!");
        }
        if ((tableCols.length != propertyTypes.length)
                || (tableCols.length != fieldNames.length)) {
            throw new EntityConvertException(
                    "table Column counts not equal property length!");
        }

        for (int i = 0; i < tableCols.length; i++) {
            int index = cursor.getColumnIndex(tableCols[i]);
            if (index != -1) {
                int type = propertyTypes[i];
                switch (type) {
                case BLOB_TYPE:
                    byte[] propertyVal1 = cursor.getBlob(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal1);
                    break;
                case SHORT_TYPE:
                    short propertyVal2 = cursor.getShort(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal2);
                    break;
                case INT_TYPE:
                    int propertyVal3 = cursor.getInt(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal3);
                    break;
                case LONG_TYPE:
                    long propertyVal4 = cursor.getLong(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal4);
                    break;

                case FLOAT_TYPE:
                    float propertyVal5 = cursor.getFloat(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal5);
                    break;
                case DOUBLE_TYPE:
                    double propertyVal6 = cursor.getDouble(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal6);
                    break;
                case STRING_TYPE:
                    String propertyVal7 = cursor.getString(index);
                    setObjVal(entity, clazz, fieldNames[i], propertyVal7);
                    break;
                }
            }
        }
    }
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值