2013-11-02 139 views
0

我試圖加載數據庫列與遊標結果爲我的測驗應用程序。這樣做的原因是,我想填充在每個類別的問題的列表視圖,所以我成立了這一點:加載數據庫列,查詢結果不起作用

public void putValues(){ 
    for (int i = 0; i < 18; i++) { 
     Cursor contentCursor = null; 
     contentCursor = mDB 
       .rawQuery("select count(*) from questions_en where used = 0 and category" + " = " + i, null); 

     if(contentCursor.getCount() >0) 
      contentCursor.moveToFirst(); 

     if (contentCursor.isAfterLast()) { 
      contentCursor.close(); 
      mDB.close(); 
      return; 
     } 
     int contentCursorInt = contentCursor.getInt(0); 
     Cursor upateCursor = null; 
     upateCursor = mDB.rawQuery("update categories_en set questions_count" + " = " + contentCursorInt + " where " + "_id" + " = " + i, null); 
     upateCursor.moveToNext(); 
     upateCursor.close(); 
     contentCursor.close(); 
    } 

} 

,這樣,當用戶點擊一個答案(問題屏幕上)使用變爲1(或任何非零值)查詢結果改變。上面的代碼第一次正常工作。因爲我還沒有設置屏幕的問題,我加入這個查詢:

public void test(){ 
    Cursor cus = mDB.rawQuery("update questions_en set used = 1 where category = 2 and _id = 146", null); 
    cus.close(); 
} 

到我的DB適配器,然後叫從我MainActivty

 @Override 
     public void onClick(View v) { 
      TestAdapter mTest = new TestAdapter(MainActivity.this); 
      mTest.createDatabase(); 
      mTest.open(); 
      mTest.test(); 
      Log.d(DBHelper.TAG, " Worked "); 
      mTest.close(); 

     } 
    }); 

這種方法但當我對這個點擊進入我的ListActivity我預計類別2的值已經改變,因爲查詢剛剛再次執行。但它不會減少。我從DDMS(文件資源管理器)中取出數據庫,並且發現對_id = 146的查詢實際上沒有更改使用爲1.任何可能的原因幫助?

+0

與[這裏]幫助(http://stackoverflow.com/questions/9798473/sqlite-in解決問題-Android-如何對更新一個特定行)。 – Jimi

回答

0

this的幫助下解決問題。

我只是改變了這一點:

public void test(){ 
    Cursor cus = mDB.rawQuery("update questions_en set used = 1 where category = 2 and _id = 146", null); 
    cus.close(); 
} 

這個

public void test(){ 
     int id = 3; 
     ContentValues data = new ContentValues(); 
     data.put(DBHelper.KEY_USED, "1"); 
     mDB.update(DBHelper.KEY_QUESTIONS_TABLE, data, DBHelper.KEY_ID + " = " + id , null); 
}