sqlite in Android Method Example for Removing Duplicate Values When Querying Data

  • 2021-11-01 04:50:35
  • OfStack

1. Mode 1:


    /**
     *  Parameter 1 : Do you want to remove weight 
     *  Parameter 2 : Table name 
     *  Parameter 3 : columns  Represents the fields of the query ,new String[]{MODEL} Indicates querying the schema in the table (also indicates the result of the query) 
     *  Parameter thinking: selection Represents the conditions of the query, PHONE_NUMBER+" = ?"  Indicates the query mode according to the mobile phone number 
     *  Parameter 5 : selectionArgs  Represents the value corresponding to the query condition ,new String[]{phoneNumber} Represents the value corresponding to the query condition 
     *  Parameter 6 : String groupBy  Grouping 
     *  Parameter 7 : String having
     *  Parameter 8 : orderBy  Indicates according to what sort ,
     *  Parameter 9 : limit  Limit the number of rows returned by the query, NULL Represents an unrestricted clause 
     **/
    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);

The full query code is as follows:


  /**
   *  Query scenic spot data according to scenic spot name 
   * @param areaName
   * @return 0 Interception mode was not found (that is, interception mode was not set for this mobile phone number)  1 : Intercept text messages  2 Intercept a call  3 : Intercept all 
   **/
  public List<ScenicSpot> getScenicAreas(String areaName){
    ArrayList<ScenicSpot> scenicSpotList = new ArrayList<>();
    String model = "0";
    SQLiteDatabase readableDatabase = mSmartTourSQLiteOpenHelper.getReadableDatabase();
    /**
     *  Parameter 1 : Do you want to remove weight 
     *  Parameter 2 : Table name 
     *  Parameter 3 : columns  Represents the fields of the query ,new String[]{MODEL} Indicates querying the schema in the table (also indicates the result of the query) 
     *  Parameter thinking: selection Represents the conditions of the query, PHONE_NUMBER+" = ?"  Indicates the query mode according to the mobile phone number 
     *  Parameter 5 : selectionArgs  Represents the value corresponding to the query condition ,new String[]{phoneNumber} Represents the value corresponding to the query condition 
     *  Parameter 6 : String groupBy  Grouping 
     *  Parameter 7 : String having
     *  Parameter 8 : orderBy  Indicates according to what sort ,
     *  Parameter 9 : limit  Limit the number of rows returned by the query, NULL Represents an unrestricted clause 
     **/
    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;
  }

Mode 2:


String sql = "select distinct "
   + TYPENAME
   + " from "
   + TABLE_NAME 
   + " ORDER BY " + TYPE + " ASC";
  Cursor c = db.rawQuery(sql, null);

Complete code:


 /**
 * @return  All organizational structure names 
 **/
 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;
 }
 }

Summarize


Related articles: