2014-02-13 209 views
0

我有一個存儲用戶的ID(col_id),名稱(col_name)和用戶名(username)的表。下面是我的代碼我用來檢索數據SQL select查詢返回列的名稱

public HashMap<String, String> getUserDetails(){ 

    HashMap<String, String> user = new HashMap<String, String>(); 
    String selectQuery = "SELECT * FROM " + TABLE_COL_DATA; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    cursor.moveToFirst(); 
    if(cursor.getCount() > 0){ 
     user.put("col_id", KEY_ID); 
     user.put("name", KEY_NAME); 
     user.put("username", KEY_USERNAME); 
    } 

    return user; 
} 

但是當我登錄檢索數據,它顯示col_id,COL_NAME和用戶名,而不是實際添加的數據。

這是我得到的數據,並記錄它

dbHandler.setCollectorTable(colID, colName, username); 
HashMap<String, String> tmpMap = dbHandler.getUserDetails(); 
Log.d("Retrieved Col ID", tmpMap.get(KEY_COL_ID)); 
Log.d("Retrieved Col Name", tmpMap.get("name")); 
Log.d("Retrieved Col Username", tmpMap.get("username")); 

回答

3

要從光標獲取數據第一。然後嘗試將數據放入HashMap象下面這樣:

if(cursor.getCount() > 0){ 
    user.put("col_id", cursor.getInt(0)); 
    user.put("name",cursor.getstring(1)); 
    user.put("username", cursor.getstring(2)); 
} 

並轉到Cursor。有很多方法可用於從光標獲取數據。

+0

工作正常。謝謝 –

+0

@ThahzanMohomed喲歡迎。 –