2013-12-20 134 views
0

我有sqlite數據庫處理程序,將在用戶登錄到應用程序時創建一個表。服務器發回一些關於作爲商店的用戶的信息。當用戶轉到下一個活動時,我需要從該數據庫中檢索一個值,並將其存儲在本地。我不確定如何檢索值5並將其存儲在活動OnCreate中。任何幫助將不勝感激。從數據庫sqlite檢索值

數據庫處理程序:

public HashMap<String, String> retrieveUser(){ 
    HashMap<String,String> user = new HashMap<String,String>(); 
    String selectQuery = "SELECT * FROM " + TABLE_LOGIN; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 
    // Move to first row 
    cursor.moveToFirst(); 
    if(cursor.getCount() > 0){ 
     user.put("first_name", cursor.getString(1)); 
     user.put("last_name", cursor.getString(2)); 
     user.put("email", cursor.getString(3)); 
     user.put("dob", cursor.getString(4)); 
     user.put("userid", cursor.getString(5)); 
     user.put("created_at", cursor.getString(6)); 
    } 
    cursor.close(); 
    db.close(); 
    // return user 
    return user; 
} 

編輯:我解決了它這種方式

dbh = new DatabaseHandler(this); 
HashMap<String, String> details = dbh.retrieveUser(); 
    String UserId = details.get("userid"); 
+0

首先閱讀SQLite數據庫的簡單的概念,這裏是參考鏈接http://chintankhetiya.wordpress.com/2013/06/01/sqlite-database-example/ –

+1

你是什麼意思'怎麼找回值5並存儲它?你想只檢索5條記錄嗎? – GrIsHu

+0

我的意思是隻檢索user.put(「userid」,cursor.getString(5)); – Carbongixxer

回答

1

如果你想在userid列retreive從表中則僅quering爲userid如下嘗試。

public HashMap<String, String> retrieveUser(){ 
    HashMap<String,String> user = new HashMap<String,String>(); 
    String selectQuery = "SELECT userid FROM " + TABLE_LOGIN; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 
    // Move to first row 
    cursor.moveToFirst(); 
    while(cursor.getCount() > 0){ 
     user.put("userid", cursor.getString(0)); 
     cursor.moveToNext(); 
     user.add("userVal",cursor.getString(0)); 
     } 
    cursor.close(); 
    db.close(); 

    return user; 
}