編輯:找到解決方案(在後的底部),仍然不知道問題是什麼,雖然在我的SQLite數據庫有一個很奇怪的錯誤(或者也許我只是沒有SQLite的光標值變化
林得到足夠的睡眠)
在輸入用戶體重,並輸入日期。如果用戶在同一天輸入兩次或更多,則最後一個輸入被覆蓋。 它似乎工作正常,但是當我拉出數據時,日期字符串由於某種原因而更改。
創建表:
String CREATE_BODYWEIGHT_TABLE = "CREATE TABLE " + TABLE_BODYWEIGHT + "(" + KEY_DATE + " TEXT PRIMARY KEY," + KEY_WEIGHT + " REAL)";
輸入和檢索:
public void insertWeight(String date, float weight){
SQLiteDatabase db = this.getWritableDatabase();
Log.d("DBHandler date", date);
db.execSQL("INSERT OR REPLACE INTO " + TABLE_BODYWEIGHT + "(date, weight) VALUES (" + date + ", " + weight + ")");
}
public HashMap<String, Float> getAllWeightEntries(){
HashMap<String, Float> dateAndWeight = new HashMap<String, Float>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_BODYWEIGHT;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Log.d("Return date", cursor.getString(0));
dateAndWeight.put(cursor.getString(0), cursor.getFloat(1));
} while (cursor.moveToNext());
}
// return map
return dateAndWeight;
}
這是logcat的輸出:
07-16 12:03:42.758: DBHandler date: 2014-07-16
07-16 12:03:42.798: Return date: 1991
07-16 12:03:42.798: Date: 1991
07-16 12:03:42.798: Weight: 123.0
編輯: 我改變了我的insertWeight到以下,現在它工作:
public void insertWeight(String date, float weight){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, date);
values.put(KEY_WEIGHT, weight);
db.insertWithOnConflict(TABLE_BODYWEIGHT, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
嘗試從設備中拉出數據庫並查看它。它存儲什麼?使用這些解決方案之一如何拉出數據庫:http://stackoverflow.com/questions/14744496/extract-database-of-an-application-from-android-device-through-adb或http:// stackoverflow。 COM /問題/ 9997976/Android的拉取的SQLite數據庫,Android的設備。 –