Android SQLite Cursors中moveToFirst()的用途是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12445010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:40:59  来源:igfitidea点击:

What is The use of moveToFirst () in SQLite Cursors

androidsqlitecursor

提问by Aswin

I am a programming newbie and I found this piece of code in the internet and it works fine

我是一个编程新手,我在互联网上找到了这段代码,它运行良好

Cursor c=db.query(DataBase.TB_NAME, new String[] {DataBase.KEY_ROWID,DataBase.KEY_RATE}, DataBase.KEY_ROWID+"= 1", null, null, null, null);
        if(c!=null)
        {
            c.moveToFirst();
        }

but I am not able to understand the use of the

但我无法理解使用

if(c!=null)
    {
        c.moveToFirst();
    }

part. What does it do exactly , and if I remove the

部分。它究竟做了什么,如果我删除

if(c!=null) { c.moveToFirst(); }

part, the code doesn't work.

部分,代码不起作用。

回答by Ted Hopp

The docs for SQLiteDatabase.query()say that the query methods return:

SQLiteDatabase.query()的文档说查询方法返回:

"A Cursor object, which is positioned before the first entry."

“一个 Cursor 对象,它位于第一个条目之前。”

Calling moveToFirst()does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty). Note that to guard against an empty return set, the code you posted should be testing the return value (which it is not doing).

调用moveToFirst()有两件事:它允许您测试查询是否返回空集(通过测试返回值)并将光标移动到第一个结果(当集不为空时)。请注意,为了防止空返回集,您发布的代码应该测试返回值(它没有这样做)。

Unlike the call to moveToFirst(), the test for if(c!=null)is useless; query()will either return a Cursorobject or it will throw an exception. It will never return null.

与调用 不同moveToFirst(),测试 forif(c!=null)是无用的;query()要么返回一个Cursor对象,要么抛出一个异常。它永远不会回来null

回答by macio.Jun

if (c.moveToFirst()) {
  while(!c.isAfterLast()) { // If you use c.moveToNext() here, you will bypass the first row, which is WRONG
    ...
    c.moveToNext();
  } 
}

回答by Bobs

Cursor is not a Row of the result of query. Cursor is an object that can iterate on the result rows of your query. Cursor can moves to each row. .moveToFirst()method move it to the first row of result table.

光标不是查询结果的行。Cursor 是一个可以迭代查询结果行的对象。光标可以移动到每一行。.moveToFirst()方法将其移动到结果表的第一行。

回答by WaterRocket8236

moveToFirst() method moves the cursor to the first row. It allows to perform a test whether the query returned an empty set or not. Here is a sample of its implementation,

moveToFirst() 方法将光标移动到第一行。它允许执行测试查询是否返回空集。这是它的实现示例,

if (cursor.getCount() == 0 || !cursor.moveToFirst()) {

return cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN)); 

cursor.close(); 

回答by hgg

what macio.Jun says is right!

macio.Jun 说的是对的!

we have code like below:

我们有如下代码:

    String sql = "select id,title,url,singer,view,info from cache where id=" + id;
    SQLiteDatabase db = getMaintainer().getReadableDatabase();
    Cursor query = db.rawQuery(sql, null);
    query.moveToFirst(); 
    while(query.moveToNext()){
        DBMusicData entity = new DBMusicData();
        entity.setId(query.getString(query.getColumnIndex(FIELD_ID)));
        entity.setTitle(query.getString(query.getColumnIndex(FIELD_TITLE)));
        entity.setSinger(query.getString(query.getColumnIndex(FIELD_SINGER)));
        entity.setTitlepic(query.getString(query.getColumnIndex(FIELD_PICURL)));
        entity.setInfoUrl(query.getString(query.getColumnIndex(FIELD_INFO)));
        entity.setViews(query.getString(query.getColumnIndex(FIELD_VIEW)));
        Log.w(tag, "cache:"+ entity.toString());
    }
    query.close();
    query=null;
    db.close();
    db=null;

If we have only one record in the cache table, query.moveToFirst();will cause that no record returns.

如果我们在缓存表中只有一条记录,query.moveToFirst(); 将导致没有记录返回。