2017-07-18 53 views
0

對於學校的Android計步器項目,我使用SQLite遵循教程。我只有一個表格,其中包含按天製作的步驟數(以字符串形式)。 問題是,無論我哪天打電話,它都會讓我2017年成爲步數。所以我想這跟我收到的日期有關。android sqlite總是在查詢中獲得2017年

這裏是處理數據庫邏輯的類:我測試我的應用程序在我的Galaxy S4迷你

public class DatabaseHandler extends SQLiteOpenHelper { 
    private static String TAG = "DatabaseHandler"; 

    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "statsManager"; 

    // Table name 
    private static final String DAILY_STATS_TABLE = "dailyStats"; 

    // Contacts Table Columns names 
    private static final String KEY_DAY = "day"; 
    private static final String KEY_STEPS = "steps"; 

    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.d(TAG, "::onCreate"); 
     String CREATE_STAT_TABLE = "CREATE TABLE " + DAILY_STATS_TABLE +     "(" + KEY_DAY + " TEXT PRIMARY KEY," + KEY_STEPS + " INTEGER" + ")"; 
     db.execSQL(CREATE_STAT_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.d(TAG, "::onUpgrade"); 
     db.execSQL("DROP TABLE IF EXISTS " + DAILY_STATS_TABLE); 
     onCreate(db); 
    } 

    public void addContact(DailyStat dailyStat) { 
     Log.d(TAG, "::addContact"); 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_DAY, dailyStat.getFormattedDate()); 
     Log.d(TAG, "trying to insert date : " + dailyStat.getFormattedDate()); 
     values.put(KEY_STEPS, dailyStat.get_steps()); 
     Log.d(TAG, "trying to insert steps : " + dailyStat.get_steps()); 

     // Inserting Row 
     db.insert(DAILY_STATS_TABLE, null, values); 
     db.close(); // Closing database connection 
    } 

    public DailyStat getDailyStat(String day) throws ParseException, NullPointerException { 
     Log.d(TAG, "::getDailyStat"); 
     Log.i(TAG, "looking for : " + String.valueOf(day)); 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.query(DAILY_STATS_TABLE, 
          new String[] {KEY_DAY, KEY_STEPS}, 
          KEY_DAY + "=?", 
          new String[] {day}, 
          null, null, null, null); 
     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     Log.i(TAG, cursor.toString()); 
     DailyStat result = null; 
     try { 
      Log.d(TAG, "got : " + cursor.getString(0) + " and " + cursor.getInt(0)); 
      result = new DailyStat(cursor.getString(0), cursor.getInt(0)); 
     } catch (CursorIndexOutOfBoundsException e) { 
      Log.i(TAG, "no stats for this day."); 
     } finally { 
      cursor.close(); 
     } 
     return result; 
    } 

// other methods 

,所以我不能輕易看到數據庫的樣子。 我聽說你看不到使用insert()和query()方法形成的請求。

任何人有什麼錯誤的想法?

+2

你不覺得,應該是'cursor.getString(0),cursor.getInt(1)' –

+1

你實際上可以很容易地看到數據庫...嘗試Stetho庫。這並不能解決這個錯誤 –

+0

可能的話,看看我假設的教程,例如,如果我有一個具有一列INT列的表作爲最後一列,它將通過cursor.getInt(0)來訪問,因爲它是表中的第一個int。 – CodingMouse

回答

0

您遇到的問題是您沒有從數據庫中獲取步驟。相反,你會得到日期的一部分。

即以下兩行

 Log.d(TAG, "got : " + cursor.getString(0) + " and " + cursor.getInt(0)); 
     result = new DailyStat(cursor.getString(0), cursor.getInt(0)); 

應該是: -

 Log.d(TAG, "got : " + cursor.getString(0) + " and " + cursor.getInt(1)); 
     result = new DailyStat(cursor.getString(0), cursor.getInt(1)); 

即索引(第2列步驟),而不是索引(所述第一柱)。

下面是您可能希望考慮採用的代碼:a)修復問題並介紹可能更靈活的技術,並提供快速解決方法,避免查看數據庫中的數據。

因此,或許下面將使用: -

1)我簡單DailyStats類(這是用於測試目的注意,它不打算使用,但已被列入讓你擁有全貌): -

public class DailyStat { 

    private String mDate; 
    private int mSteps; 

    public DailyStat(String date, int steps) { 
     mDate = date; 
     mSteps = steps; 
    } 

    public String getFormattedDate() { 
     return mDate; 
    } 

    public int get_steps() { 
     return mSteps; 
    } 
} 

2)改變/新DBHelper方法: -

 public DailyStat getDailyStat(String day) throws ParseException, NullPointerException { 

      DailyStat result = null; 
      Log.d(TAG, "::getDailyStat"); 
      //Log.i(TAG, "looking for : " + String.valueOf(day)); 
      Log.i(TAG, "looking for : " + day); 
      SQLiteDatabase db = this.getReadableDatabase(); 
      Cursor cursor = db.query(DAILY_STATS_TABLE, 
        new String[] {KEY_DAY, KEY_STEPS}, 
        KEY_DAY + "=?", 
        new String[] {day}, 
        null, null, null, null); 
      if (cursor.getCount() > 0) { 
       cursor.moveToFirst(); 
       //Log.d(TAG, "got : " + cursor.getString(0) + " and " + cursor.getInt(0)); 
       Log.d(TAG, "got : " + cursor.getString(cursor.getColumnIndex(KEY_DAY)) 
         + " and " 
         + cursor.getInt(cursor.getColumnIndex(KEY_STEPS)) 
       ); 

       //result = new DailyStat(cursor.getString(0), cursor.getInt(0)); 
       result = new DailyStat(cursor.getString(cursor.getColumnIndex(KEY_DAY)), 
         cursor.getInt(cursor.getColumnIndex(KEY_STEPS)) 
       ); 
      } else { 
       Log.i(TAG, "no stats for this day."); 
      } 
      cursor.close(); 
      return result; 
     } 

     public Cursor getAllStats() { 
      SQLiteDatabase db = this.getReadableDatabase(); 
      return db.query(DAILY_STATS_TABLE,null,null,null,null,null,null); 
     } 

     public void logAllStats(Cursor cursor) { 
      while (cursor.moveToNext()) { 
       Log.d(TAG,"Row " + Integer.toString(cursor.getPosition())); 
       for (int i =0; i < cursor.getColumnCount();i++) { 
        Log.d(TAG, "\tColumn=" + cursor.getColumnName(i) + 
          " Value=" + cursor.getString(i) 
        ); 
       } 
      } 
     } 

注意!基本上,我在getDailyStat中完成的所有操作都是利用cursor.getCount而不是檢查空遊標(遊標不爲空id 0行返回),並使用cursor.getColumnIndex(columnname),後者解析不正確的索引(按照第一個註釋)。

getAllStatslogAllStats只是作爲快速瞭解無法看到數據庫的問題而添加的。

3)用於測試調用活動代碼(注:我叫類數據庫處理器StepsDBHelper我來回方便): -

  //DailyStat ds = new DailyStat("21/03/2017",10); used for 1st run 
      //DailyStat ds = new DailyStat("21/03/2016",10); used for 2nd run 

      StepsDBHelper sdbhlp = new StepsDBHelper(this); 
      //sdbhlp.addContact(ds); used for 1st and then 2nd run 
      DailyStat ds1 = sdbhlp.getDailyStat("21/03/2016"); 
      Log.d("DS1","Day=" + ds1.getFormattedDate() + " Steps=" + Integer.toString(ds1.get_steps())); 
      DailyStat ds2 = sdbhlp.getDailyStat("21/03/2017"); 
      Log.d("DS2","Day=" + ds2.getFormattedDate() + " Steps=" + Integer.toString(ds2.get_steps())); 
      // using 21/03/2018 returns null DailyStats object 
      DailyStat ds3 = sdbhlp.getDailyStat("21/03/2018"); 
      if (ds3 == null) { 
       Log.d("DS3","NULL"); 
      } else { 
       Log.d("DS3","Day=" + ds3.getFormattedDate() + " Steps=" + Integer.toString(ds3.get_steps())); 
      } 
      sdbhlp.logAllStats(sdbhlp.getAllStats()); 

最後從加入前兩個運行後的輸出的數據(如每註釋): -

07-19 07:56:34.190 3981-3981/? D/DatabaseHandler: ::getDailyStat 
07-19 07:56:34.190 3981-3981/? I/DatabaseHandler: looking for : 21/03/2016 
07-19 07:56:34.191 3981-3981/? D/DatabaseHandler: got : 21/03/2016 and 10 
07-19 07:56:34.191 3981-3981/? D/DS1: Day=21/03/2016 Steps=10 
07-19 07:56:34.191 3981-3981/? D/DatabaseHandler: ::getDailyStat 
07-19 07:56:34.191 3981-3981/? I/DatabaseHandler: looking for : 21/03/2017 
07-19 07:56:34.191 3981-3981/? D/DatabaseHandler: got : 21/03/2017 and 10 
07-19 07:56:34.191 3981-3981/? D/DS2: Day=21/03/2017 Steps=10 
07-19 07:56:34.191 3981-3981/? D/DatabaseHandler: ::getDailyStat 
07-19 07:56:34.191 3981-3981/? I/DatabaseHandler: looking for : 21/03/2018 
07-19 07:56:34.194 3981-3981/? I/DatabaseHandler: no stats for this day. 
07-19 07:56:34.195 3981-3981/? D/DS3: NULL 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Row 0 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Column=day Value=21/03/2017 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Column=steps Value=10 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Row 1 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Column=day Value=21/03/2016 
07-19 07:56:34.195 3981-3981/? D/DatabaseHandler: Column=steps Value=10 

代替2017(21與I使用的日期格式)作爲預期的步驟的數量。

+0

謝謝你的完整答案。 昨天晚上,我用getInt(1)替換了.getInt(0)的評論,它工作正常。 但看到你的帖子,我想我會再次糾正它,並使用cursor.getColumnIndex(KEY)它似乎是一個更好的做法。 – CodingMouse