新聞中心
小編給大家分享一下Android中如何實(shí)現(xiàn)sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、網(wǎng)絡(luò)營(yíng)銷推廣、小程序制作、微信公眾號(hào)開(kāi)發(fā)、成都網(wǎng)站推廣服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為酒店設(shè)計(jì)企業(yè)提供源源不斷的流量和訂單咨詢。
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 ListgetScenicAreas(String areaName){ ArrayList 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 ListqueryTypeNames() { synchronized (DatabaseHelper.lock) { List 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 (); } if (type != null && type.length() > 1) { types.add(type); } } db.close(); return types; } catch (Exception e) { db.close(); } return types; } }
以上是“Android中如何實(shí)現(xiàn)sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:Android中如何實(shí)現(xiàn)sqlite查詢數(shù)據(jù)時(shí)去掉重復(fù)值
網(wǎng)頁(yè)地址:http://www.dlmjj.cn/article/peoohh.html