dimanche 19 juillet 2015

use of distinct for a particular column in sqlite query does not show distinct values

I have written a query in sqlite to get distinct values from a particular column , but the values which i am getting inside spinner are not distinct , when i remove _id from the query i am getting an error saying "column _id does not exist" , but when i include _id in the query the spinner gets populated with values but not distinct.

public Cursor getAccName(String fromdate,String todate){
    SQLiteDatabase db = helper.getWritableDatabase();
    Cursor cursor = db.rawQuery("Select DISTINCT 
    "+DbListHelper.ACCOUNT_NAME+","+DbListHelper.UID+
    " from "+DbListHelper.TABLE_NAME_FINALDATA+" where  
    "+DbListHelper.EFFECTIVE_DATE+" between '" + 
    fromdate + "' and '"+ todate +"'", null);
    return cursor;
}

view_records.java

listHelper = new SqliteVehicleDetails(getBaseContext());
listHelper.open(getBaseContext());
etFromDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(DATE_PICKER_ID);
        }
    });

    etTodate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(DATE_PICKER_ID_1);

        }
    });

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_PICKER_ID:
        return new DatePickerDialog(this, datePickerListener, year, month, 
        day);

    case DATE_PICKER_ID_1:
        return new DatePickerDialog(this, datePickerListener1, year1, 
        month1, day1);
    }
    return null;

}

private DatePickerDialog.OnDateSetListener datePickerListener = new 
    DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
         String dt;
         year = selectedYear;
         month = selectedMonth + 1;
         day = selectedDay;

        try {
            dt = day + "/" + month + "/" + year;

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
            Date currentDt = null;          
            currentDt = sdf.parse(dt);
            String d = sdf.format(currentDt);
            etFromDate.setText(d);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
};

private DatePickerDialog.OnDateSetListener datePickerListener1 = new 
    DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
         String dt;
         year1 = selectedYear;
         month1 = selectedMonth + 1;
         day1 = selectedDay;

        try {
            dt = day1 + "/" + month1 + "/" + year1;

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
            Date currentDt = null;          
            currentDt = sdf.parse(dt);
            String d = sdf.format(currentDt);
            etTodate.setText(d);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
};

btnShow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            cursor = listHelper.getFinalData();
            if(cursor.moveToFirst()){
                fromDt = etFromDate.getText().toString();
                toDt = etTodate.getText().toString();
                cursor = listHelper.getAccName(fromDt,toDt);
                int[] to5 = new int[] { android.R.id.text1 };
                String[] from5 = new String[] { DbListHelper.ACCOUNT_NAME };
                adapter5 = new SimpleCursorAdapter(
                        getBaseContext(), 
                        android.R.layout.simple_list_item_1, cursor,
                        from5, to5);
                adapter5.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
                spinnerAccountName.setAdapter(adapter5);
    }

I want to know why the distinct keyword is not working and why is it necessary to include _id in the query ? Thank you

Aucun commentaire:

Enregistrer un commentaire