SQLite查询语句中的count(*) 与getColumnIndex("count")

关键代码如下:

    int count = 0;
    SQLiteDatabase db = null;
    Cursor cursor = null;

        //执行创建数据库或是表的语句
        db = openHelper.getReadableDatabase();
        //执行通过用户名和密码的参数查询出用户,并保存在cursor中
        cursor = db
                .rawQuery(
                        "select count(*) as count from student where name=? and pwd=?",
                        new String[] { name, pwd });
        //得到的用户信息通过moveToNext()方法,while语句循环输出
        while (cursor.moveToNext()) {
            count = cursor.getInt(cursor.getColumnIndex("count"));

}
问题:while语句中(即倒数第二段中的),cursor.getColumnIndex("count")语句,查询出来的是什么东西啊?getColumnIndex(string name),不是根据 name的名称获得它的列索引。可是rawQuery查询语句中,“select count(*) as count from student...”。

因为sql语句中用count(*) as count
count是一个新列的别名。这样你getColumnIndex就可以获取count列的数据。

得到count这个字段的值,,,游标指向的,,

因为getstring、getint等方法需要的参数是**列索引**
所以,先用getColumnIndex根据 name的名称获得它的列索引