想从光标中获取一行

时间:2012-11-16 17:32:22

标签: android

我想获取一行,此行有2列,其中usernamepassword的代码位于此处:

String selection = PROFILE_COLUMN_USERNAME + " = '" + userName+ "' AND " +PROFILE_COLUMN_PASSWORD + " = '" + password + "'";
Cursor cursor = database.query(TABLE_NAME, new String[] {PROFILE_COLUMN_USERNAME, PROFILE_COLUMN_PASSWORD }, selection,null, null, null, null);

我的数据库中有username = erdempassword= c等示例数据,但是当我编写此示例并写入以获取username时,这样:

String s =cursor.getString(cursor.getColumnIndex(PROFILE_COLUMN_USERNAME));

它不起作用。为什么呢?

3 个答案:

答案 0 :(得分:0)

使用moveToFirstread the documentation

答案 1 :(得分:0)

尝试类似下面的内容:此处“path_on_server”是数据库表中要提取其值的列的名称。您可以将其更改为表格的列名称。

        String query = "some query";
        Cursor c = newDB.rawQuery(query, null);

        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    mPathOnServer = c.getString(c
                            .getColumnIndex("path_on_server"));
                } while (c.moveToNext());
            }
        }
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(),
                "Could not extract information from DB");
    }

答案 2 :(得分:0)

试试这种方式......

Cursor cursor = null;

try {
    String queryString = "SELECT * FROM Table_Name";

    cursor = myDatabase.rawQuery(queryString, null);
            if (cursor != null && cursor.moveToFirst()) {

                do {
                    String nextUser = new String(cursor.getString(cursor
                            .getColumnIndex("driver_fname")));

                } while (cursor.moveToNext());

            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.deactivate();
                cursor.close();
                cursor = null;
            }
            if (myDatabase != null) {
                myDatabase.close();
            }
        }