Android中的sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例
1、方式一:
/** * 參數(shù)一:是否去重 * 參數(shù)二:表名 * 參數(shù)三:columns 表示查詢的字段,new String[]{MODEL}表示查詢?cè)摫懋?dāng)中的模式(也表示查詢的結(jié)果) * 參數(shù)思:selection表示查詢的條件,PHONE_NUMBER+" = ?" 表示根據(jù)手機(jī)號(hào)去查詢模式 * 參數(shù)五:selectionArgs 表示查詢條件對(duì)應(yīng)的值,new String[]{phoneNumber}表示查詢條件對(duì)應(yīng)的值 * 參數(shù)六:String groupBy 分組 * 參數(shù)七:String having * 參數(shù)八:orderBy 表示根據(jù)什么排序, * 參數(shù)九:limit 限制查詢返回的行數(shù),NULL表示無(wú)限制子句 **/ 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);
全部查詢代碼如下:
/** * 根據(jù)景區(qū)名稱查詢景點(diǎn)數(shù)據(jù) * @param areaName * @return 0:未查詢到攔截模式(也就是該手機(jī)號(hào)沒(méi)有設(shè)置攔截模式) 1:攔截短信 2:攔截電話 3:攔截所有 **/ public List<ScenicSpot> getScenicAreas(String areaName){ ArrayList<ScenicSpot> scenicSpotList = new ArrayList<>(); String model = "0"; SQLiteDatabase readableDatabase = mSmartTourSQLiteOpenHelper.getReadableDatabase(); /** * 參數(shù)一:是否去重 * 參數(shù)二:表名 * 參數(shù)三:columns 表示查詢的字段,new String[]{MODEL}表示查詢?cè)摫懋?dāng)中的模式(也表示查詢的結(jié)果) * 參數(shù)思:selection表示查詢的條件,PHONE_NUMBER+" = ?" 表示根據(jù)手機(jī)號(hào)去查詢模式 * 參數(shù)五:selectionArgs 表示查詢條件對(duì)應(yīng)的值,new String[]{phoneNumber}表示查詢條件對(duì)應(yīng)的值 * 參數(shù)六:String groupBy 分組 * 參數(shù)七:String having * 參數(shù)八:orderBy 表示根據(jù)什么排序, * 參數(shù)九:limit 限制查詢返回的行數(shù),NULL表示無(wú)限制子句 **/ 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 所有組織結(jié)構(gòu)名稱 **/ 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; } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android語(yǔ)音識(shí)別技術(shù)詳解及實(shí)例代碼
這篇文章主要介紹了Android語(yǔ)音識(shí)別技術(shù)的相關(guān)資料,并附實(shí)例代碼及實(shí)例實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-09-09Android編程實(shí)現(xiàn)抽屜效果的方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)抽屜效果的方法,結(jié)合具體實(shí)例形式分析了Android抽屜效果的布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06Android ScrollView實(shí)現(xiàn)下拉彈回動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了Android ScrollView實(shí)現(xiàn)下拉彈回動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android編程簡(jiǎn)單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法,涉及Android針對(duì)背景圖相關(guān)屬性設(shè)置的操作技巧,需要的朋友可以參考下2015-12-12Android中外接鍵盤的檢測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了Android中外接鍵盤的檢測(cè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Android使用Volley實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了Android使用Volley實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Kotlin中協(xié)程的創(chuàng)建過(guò)程詳析
使用協(xié)程的專業(yè)開(kāi)發(fā)者中有超過(guò) 50% 的人反映使用協(xié)程提高了工作效率,下面這篇文章主要給大家介紹了關(guān)于Kotlin中協(xié)程創(chuàng)建過(guò)程的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01Android仿網(wǎng)易一元奪寶客戶端下拉加載動(dòng)畫(huà)效果(一)
本文通過(guò)一個(gè)demo給大家介紹了android仿網(wǎng)易一元奪寶客戶端下拉加載動(dòng)畫(huà)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09