2015-07-20 70 views
-1

我已經在sqlite中編寫了一個查詢以從特定列中獲取不同的值,但是我在裏面的微調器中獲取的值並不是獨特的,當我刪除_id從查詢我得到一個錯誤,說「列_id不存在」,但是當我在查詢中包含_id微調獲取填充值但不明顯。使用獨特的特定列在sqlite查詢中不顯示不同的值

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

我想知道爲什麼不同的關鍵字不工作,爲什麼它需要在查詢中包括_id? 謝謝

+0

這是沒有必要包括_id列,你可能得到錯誤,因爲您將嘗試從光標獲取_id列的值。在選擇查詢中使用_id時,由於_id是主鍵,因此帳戶名稱和_id的組合總是不同的。 –

+0

我已經包含代碼先生,我從btnshow點擊光標獲取值 –

回答

1

微調需要一個名爲_id的唯一ID列,但是當您只是從數據庫中取出此列時,所有結果行都是唯一的。

從數據庫中,組對列中獲取不同的名稱,然後乘組中的行之一的_id值:

SELECT AccountName, 
     MIN(_id) AS _id 
FROM FinalData 
WHERE EffectiveDate BETWEEN ? AND ? 
GROUP BY AccountName