2013-09-01 63 views
0

我有這個函數應該從數據庫中通過ID返回對象。儘管請求的ID有數據,但不會返回任何數據。有人能幫我解決這個問題嗎?SQLITE不返回請求的數據

代碼:

public ChildRecord getChild(int id) { 
     ChildRecord child = null; 
     Log.v("--", "success13s "); 
     Cursor c = database.rawQuery("SELECT * FROM child " + "WHERE childID=" 
       + id, null); 
     Log.v("--", "success1 2"); 
     if (c != null) 
      if (c.moveToFirst()) { 
       do { 
        int childid = c.getInt(0); 
        String parent1 = c.getString(1); 
        String parent2 = c.getString(2); 
        String name = c.getString(3); 
        String dob = c.getString(4); 
        int gender = c.getInt(5); 
        String height = c.getString(6); 
        String weight = c.getString(7); 
        String etnicity = c.getString(8); 
        int type = c.getInt(9); 
        child = new ChildRecord(childid, name, dob, gender, height, 
          weight, etnicity, type, parent1, parent2); 
       } while (c.moveToNext()); 
      } 
     Log.v("--", "success1 "); 
     return child; 
    } 

的問題是在該行

Cursor c = database.rawQuery("SELECT * FROM child " + "WHERE childID=" 
        + id, null); 
+0

'id'在那一行有什麼值?這個值是否存在於表中? – Simon

+0

是的,但我試圖找出有其他錯誤.. –

+0

數據庫中的'childID'的類型是什麼?隱式類型轉換(例如,從'blob'類型到'int')可能會導致意想不到的結果。 – Trinimon

回答

0

我想明確這一點首先,如果你有整的「childID的」列「子」表,並有數據通過您傳入的id。查詢字符串沒有任何問題。

那麼你應該檢查:

  1. 你所指的「正確」的數據庫

  2. 嘗試執行SQLite中DB經理這個查詢,看看是否這個給出的結果。

  3. 如果您在SQLite數據庫管理器中有結果,但是在代碼中沒有得到任何結果。該問題必須來自您的「數據庫」對象。

請嘗試告訴我你的結果。