SQLite Cursors中moveToFirst()的用途是什么 [英] What is The use of moveToFirst () in SQLite Cursors

查看:23
本文介绍了SQLite Cursors中moveToFirst()的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但我无法理解

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

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

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

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

部分,代码不起作用.

推荐答案

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

一个 Cursor 对象,它位于第一个条目之前."

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

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

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()的调用不同,对if(c!=null)的测试是无用的;query() 要么返回一个 Cursor 对象,要么抛出异常.它永远不会返回 null.

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

这篇关于SQLite Cursors中moveToFirst()的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆