GreenDao使用sql语句(Order by CASE WHEN)

GreenDao使用sql语句详解

public List<VendorBean> getProviderInfo(String condition,boolean brate, int condition1,int mark1,
                                            int mark2, String ztName) {
        List<VenBean> beans = new ArrayList<>();
        try {
            String sql = "SELECT * FROM " + VenBeanDao.TABLENAME + " WHERE " +
                    "ZNAME" + " = '" +
                    ztName + "' AND " + "B_RATE" +
                    " = '" + (brate? 1 : 0) +
                    "' AND (" + "MARK" +
                    " = '" + mark1 + "' or " + "MARK" +
                    " = '" + mark2 + "') "
                    + " Order by CASE WHEN (" + "NAME_C" +
                    " like '" + condition + "%')" +
                    "THEN 0 WHEN (" + "ZJF" + " like '" + condition
                    + "%') THEN 1 WHEN (" + "WBH" + " like '" +
                    condition1+ "%') THEN 2 else 3 END,length(" + "NAME_C"
                    + ")," + "ZJF" + "," + "WBH" +
                    " LIMIT " + 50;
            Cursor cursor = mManager.getDaoSession().getDatabase().rawQuery(sql, null);
            addGetBeans(beans, cursor);
        } catch (SQLException ignored) {
        }
        return beans;
    }
//将游标里面查询出来的对象转化到list中
private void addGetBeans(List<VenBean> beans, Cursor cursor) {
        int vendorInnoIndex = cursor.getColumnIndex(VenBeanDao.Properties.Vendorinno.columnName);
        int nameCIndex = cursor.getColumnIndex(VenBeanDao.Properties.NameC.columnName);
        int nameEIndex = cursor.getColumnIndex(VenBeanDao.Properties.NameE.columnName);
        int zjfIndex = cursor.getColumnIndex(VenBeanDao.Properties.Zjf.columnName);
        int wbhIndex = cursor.getColumnIndex(VenBeanDao.Properties.Wbh.columnName);
        int ventifyMarkIndex = cursor.getColumnIndex(VenBeanDao.Properties.VentifyMark.columnName);
        int bConglomerateIndex = cursor.getColumnIndex(VenBeanDao.Properties.BConglomerate.columnName);
        int conglomerateIndex = cursor.getColumnIndex(VenBeanDao.Properties.Conglomerate.columnName);
        int mobileIndex = cursor.getColumnIndex(VenBeanDao.Properties.Mobile.columnName);
        int telNoIndex = cursor.getColumnIndex(VenBeanDao.Properties.TelNo.columnName);
        int connectManIndex = cursor.getColumnIndex(VenBeanDao.Properties.ConnectMan.columnName);
        int corpIDIndex = cursor.getColumnIndex(VenBeanDao.Properties.CorpID.columnName);
        int vendorNoIndex = cursor.getColumnIndex(VenBeanDao.Properties.VendorNo.columnName);
        while (cursor.moveToNext()) {
            VenBeanDao bean = new VenBeanDao();
            bean.setVendorinno(cursor.getInt(vendorInnoIndex));
            bean.setNameC(cursor.getString(nameCIndex));
            bean.setNameE(cursor.getString(nameEIndex));
            bean.setZjf(cursor.getString(zjfIndex));
            bean.setWbh(cursor.getString(wbhIndex));
            bean.setVentifyMark(cursor.getInt(ventifyMarkIndex));
            bean.setBConglomerate(cursor.getInt(bConglomerateIndex) != 0);
            bean.setConglomerate(cursor.getInt(conglomerateIndex));
            bean.setMobile(cursor.getString(mobileIndex));
            bean.setTelNo(cursor.getString(telNoIndex));
            bean.setConnectMan(cursor.getString(connectManIndex));
            bean.setCorpID(cursor.getString(corpIDIndex));
            bean.setVendorNo(cursor.getString(vendorNoIndex));
            beans.add(bean);
        }
    }
点赞