2015-10-31 60 views
1

ID列值我如何循環從users表,其中通過生成的「ID」得到id列值:光標登錄活動

id integer primary key autoincrement 

我有這樣的記錄在我的表:

id  name 

1  Me 

2  You 

這裏是我覺得我應該嘗試:

public String[] getUsersId() { 

    String selectQuery = "SELECT id FROM " + TABLE_USERS; 
      SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.rawQuery(selectQuery, null); 
      String[] data = null; 

    if (cursor.moveToFirst()) { 
     do { 

     } while (cursor.moveToNext()); 
    } 

    db.close(); 
    return data; 
} 

現在,我想LIK e知道如何在我的活動中記錄所有「id」?

+0

只需使用'DatabaseUtils.dumpCursor',問題是:你在做什麼螞蟻返回'getUsersId'方法(以及爲什麼)?你想用String []做什麼? – pskink

回答

2

使用cursor.getInt擺脫光標int值:

if (cursor.moveToFirst()) { 
     do { 
      int id_row=cursor.getInt(cursor.getColumnIndex("id")); 
      //int id_row=cursor.getInt(0); 
      Log.d("TAG","id is ::"+id_row); 
     } while (cursor.moveToNext()); 
    } 
+0

謝謝。我已經勾選你的答案有幫助,並會在3分鐘內接受,但我仍然有疑問執行進一步的行動,我必須在我的活動本身使用光標,現在我只是這樣做:dh.getUsersId(); – Sophie

+0

@Sophie:請使用數據結構來獲取Activity中的數據,如ArrayList –

0
You can try in this way 
    if (cursor.moveToFirst()) 
    { 
     do 
     { 
      int idIndex = cursor.getColumnIndex("id"); 
      if (ideIndex != -1) 
      { 
       int ID = cursor.getInt(idIndex); 
      } 
     } while (cursor.moveToNext()); 
    } 
0

試試這個,

請檢查這種情況下,如果你的cursor.getCount> 0

if (cursor.getCount() > 0) { 
       cursor.moveToFirst(); 
       do { 
        String strId = cursor.getString(cursor.getColumnIndex("yourid"); 

       } while (cursor.moveToNext()); 
      }