0

我想得到一行,这一行有 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));

它不起作用。为什么?

4

3 回答 3

0

使用moveToFirst阅读文档

于 2012-11-16T17:34:14.117 回答
0

尝试如下操作:这里的“path_on_server”是正在提取其值的 DB 表中的列的名称。您可以将其更改为表的列名。

        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");
    }
于 2012-11-16T17:37:13.660 回答
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();
            }
        }
于 2012-11-16T17:52:35.787 回答