在 activity 中记录 id 列值的游标

Cursor to log id column value in activity

如何循环从 users table 中获取 id 列值,其中 "id" 通过 :

生成
id integer primary key autoincrement

我的 table:

中有这样的记录
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" 记录到我的 activity 中?

使用 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());
    }

你可以这样试试

    if (cursor.moveToFirst()) 
    {
        do
        {
            int idIndex = cursor.getColumnIndex("id");
            if ( ideIndex != -1)
            {
                int ID = cursor.getInt(idIndex);
            }
        } while (cursor.moveToNext());
    }

试试这个,

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

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

                } while (cursor.moveToNext());
            }