1 . 全部結果查詢
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 取得所有資料
*
* @return
*/
public List<Map<String, Object>> getAllData() {
List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
// 查詢條件
String strSelect = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(strSelect, null);
if (cursor.moveToFirst()) {
do {
Map<String, Object> mapData = new HashMap<String, Object>();
mapData.put(KEY_ID, cursor.getString(0));
mapData.put(KEY_DATE, cursor.getString(1));
mapData.put(KEY_NUMBER, cursor.getString(2));
mapData.put(KEY_NOTE, cursor.getString(3));
listData.add(mapData);
} while (cursor.moveToNext());
}
return listData;
}
2 . 查詢條件結果
private static final String TABLE_CONTACTS = "counter";
private static final String KEY_DATE = "count_date";
private static final String KEY_NUMBER = "count_number";
private static final String KEY_NOTE = "count_note";
/**
* 取得當日資料
*
* @return
*/
public Map<String, Object> getCurrentData(@NonNull Date mDate) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Map<String, Object> mapData = new HashMap<String, Object>();
// 查詢條件
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE " + KEY_DATE + " = ? ";
String[] strVal = new String[]{sdf.format(mDate)};
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, strVal);
if (cursor.moveToFirst()) {
do {
mapData.put(KEY_ID, cursor.getString(0));
mapData.put(KEY_DATE, cursor.getString(1));
mapData.put(KEY_NUMBER, cursor.getString(2));
mapData.put(KEY_NOTE, cursor.getString(3));
} while (cursor.moveToNext());
}
return mapData;
}
查詢結果出來的容器 Cursor 是什麼?我要怎麼藉由此東西取出我要的值?
官方說明為
還是不懂怎辦?
可以把它想成是一種 Map
取出來的順序必須依照欄位的順序
不然會取不到對應的值
參考資料:時間區間格式
歡迎轉載,請註明出處。
沒有留言:
張貼留言