时间:2020-12-28 11:41:14 | 栏目:Android代码 | 点击:次
1、方式一:
/** * 参数一:是否去重 * 参数二:表名 * 参数三:columns 表示查询的字段,new String[]{MODEL}表示查询该表当中的模式(也表示查询的结果) * 参数思:selection表示查询的条件,PHONE_NUMBER+" = ?" 表示根据手机号去查询模式 * 参数五:selectionArgs 表示查询条件对应的值,new String[]{phoneNumber}表示查询条件对应的值 * 参数六:String groupBy 分组 * 参数七:String having * 参数八:orderBy 表示根据什么排序, * 参数九:limit 限制查询返回的行数,NULL表示无限制子句 **/ Cursor cursor = readableDatabase.query(true,TABLE_NAME, new String[]{DESCRIPTION,ID,IMAGE_URL,LATITUDE,LONGITUDE,NAME,NEED_AUDIO,SPOT_TYPE,TGROUP,AUDIO_NAME,AREA_NAME}, AREA_NAME + " = ?", new String[]{areaName}, null, null, null,null);
全部查询代码如下:
/** * 根据景区名称查询景点数据 * @param areaName * @return 0:未查询到拦截模式(也就是该手机号没有设置拦截模式) 1:拦截短信 2:拦截电话 3:拦截所有 **/ public List<ScenicSpot> getScenicAreas(String areaName){ ArrayList<ScenicSpot> scenicSpotList = new ArrayList<>(); String model = "0"; SQLiteDatabase readableDatabase = mSmartTourSQLiteOpenHelper.getReadableDatabase(); /** * 参数一:是否去重 * 参数二:表名 * 参数三:columns 表示查询的字段,new String[]{MODEL}表示查询该表当中的模式(也表示查询的结果) * 参数思:selection表示查询的条件,PHONE_NUMBER+" = ?" 表示根据手机号去查询模式 * 参数五:selectionArgs 表示查询条件对应的值,new String[]{phoneNumber}表示查询条件对应的值 * 参数六:String groupBy 分组 * 参数七:String having * 参数八:orderBy 表示根据什么排序, * 参数九:limit 限制查询返回的行数,NULL表示无限制子句 **/ Cursor cursor = readableDatabase.query(true,TABLE_NAME, new String[]{DESCRIPTION,ID,IMAGE_URL,LATITUDE,LONGITUDE,NAME,NEED_AUDIO,SPOT_TYPE,TGROUP,AUDIO_NAME,AREA_NAME}, AREA_NAME + " = ?", new String[]{areaName}, null, null, null,null); while (cursor.moveToNext()){ ScenicSpot scenicSpot = new ScenicSpot(); String description = cursor.getString(cursor.getColumnIndex(DESCRIPTION)); String id = cursor.getString(cursor.getColumnIndex(ID)); String image_url = cursor.getString(cursor.getColumnIndex(IMAGE_URL)); String latitude = cursor.getString(cursor.getColumnIndex(LATITUDE)); String longitude = cursor.getString(cursor.getColumnIndex(LONGITUDE)); String name = cursor.getString(cursor.getColumnIndex(NAME)); String need_audio = cursor.getString(cursor.getColumnIndex(NEED_AUDIO)); String spot_type = cursor.getString(cursor.getColumnIndex(SPOT_TYPE)); String tgroup = cursor.getString(cursor.getColumnIndex(TGROUP)); String audio_name = cursor.getString(cursor.getColumnIndex(AUDIO_NAME)); String area_name = cursor.getString(cursor.getColumnIndex(AREA_NAME)); scenicSpot.setDescription(description); scenicSpot.setId(id); scenicSpot.setImageurl(image_url); scenicSpot.setLatitude(latitude); scenicSpot.setLongitude(longitude); scenicSpot.setName(name); scenicSpot.setNeedAudio(need_audio); scenicSpot.setSpotType(spot_type); scenicSpot.setTgroup(tgroup); scenicSpot.setAudioname(audio_name); scenicSpot.setAreaName(area_name); scenicSpotList.add(scenicSpot); } cursor.close(); readableDatabase.close(); return scenicSpotList; }
方式二:
String sql = "select distinct " + TYPENAME + " from " + TABLE_NAME + " ORDER BY " + TYPE + " ASC"; Cursor c = db.rawQuery(sql, null);
完整代码:
/** * @return 所有组织结构名称 **/ public static List<String> queryTypeNames() { synchronized (DatabaseHelper.lock) { List<String> types = null; SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase(); try { String sql = "select distinct " + TYPENAME + " from " + TABLE_NAME + " ORDER BY " + TYPE + " ASC"; Cursor c = db.rawQuery(sql, null); while (c.moveToNext()) { String type = c.getString(c.getColumnIndex(TYPENAME)); if (types == null) { types = new ArrayList<String>(); } if (type != null && type.length() > 1) { types.add(type); } } db.close(); return types; } catch (Exception e) { db.close(); } return types; } }
总结