用于记录活动中id列值的游标

时间:2015-10-31 06:54:00

标签: java android sqlite cursor android-cursor

如何循环从id表中获取users列值,其中" 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;
}

现在,我想知道如何记录所有" id"在我的活动中?

3 个答案:

答案 0 :(得分:2)

使用cursor.getInt从Cursor获取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());
    }

答案 1 :(得分: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());
    }

答案 2 :(得分:0)

试试这个,

请检查此条件,如果您的cursor.getCount> 0

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

                } while (cursor.moveToNext());
            }