Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 要从游标中获取行吗_Android - Fatal编程技术网

Android 要从游标中获取行吗

Android 要从游标中获取行吗,android,Android,我想得到一行,这一行有两列,分别命名为username和password我的代码在这里: String selection = PROFILE_COLUMN_USERNAME + " = '" + userName+ "' AND " +PROFILE_COLUMN_PASSWORD + " = '" + password + "'"; Cursor cursor = database.query(TABLE_NAME, new String[] {PROFILE_COLUMN_USERNAME

我想得到一行,这一行有两列,分别命名为
username
password
我的代码在这里:

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=erdem
password=c
,但当我编写此示例并编写以获取
username
时,如下所示:

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

它不起作用。为什么?

使用
moveToFirst

尝试以下操作:此处“服务器上的路径”是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");
    }
这样试试

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

一点提示:两个
if
s是不必要的,只需使用
if(c!=null&&c.moveToFirst()){…}
,if is c is null moveToFirst将不会被调用。我对安卓是新手。感谢您的回复,但mPathOnServer的意思是?它只是一个变量的名称,我在其中存储应用程序数据库中某个表中某列的值。如果PROFILE\u column\u USERNAME是表中的实际列名,请在执行getColumnIndexit时将其用双引号引起来,谢谢。我发现了我的错误。我想给你的答案打分,但我的声誉不允许:)我可以使用SELECT*FROM Table_Name查询列出我的数据,但当我尝试查询问题时,它不起作用。可能是我问错了?