在sqlite查询中对特定列使用distinct不会显示不同的值

时间:2015-07-20 05:41:59

标签: android sqlite

我在sqlite中编写了一个查询来从特定列中获取不同的值,但是我在spinner中获取的值并不明显,当我从查询中删除_id时,我收到错误说"列_id不存在" ,但是当我在查询中包含_id时,微调器将填充值但不是不同的。

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);
    }

我想知道为什么distinct关键字不起作用,为什么有必要在查询中包含_id? 谢谢

1 个答案:

答案 0 :(得分:1)

微调器需要一个名为_id的唯一ID列,但是当您从数据库中获取此列时,所有生成的行都是唯一的。

要从数据库中获取不同的名称,请对该列进行分组,然后获取该组中某个行的SELECT AccountName, MIN(_id) AS _id FROM FinalData WHERE EffectiveDate BETWEEN ? AND ? GROUP BY AccountName 值:

zen = App.open('Notepad')
if (zen != 'None'):
    print('Program is installed!')
    wait(2)
    # Close Notepad again. 
    App.close('Notepad')
else:
    print('Program is not installed!')