什么是SQLite中的游标使用moveToFirst()的 [英] What is The use of moveToFirst () in SQLite Cursors
问题描述
我是编程新手 我发现这块code在互联网上,并能正常工作
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(); }
一部分,code不起作用。
part, the code doesn't work.
推荐答案
对于文档<一href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29">SQLiteDatabase.query()说的查询方法返回:
The docs for SQLiteDatabase.query() say that the query methods return:
A光标对象,它位于第一条目之前。
"A Cursor object, which is positioned before the first entry."
调用 moveToFirst()
做两件事情:它可以让你测试查询是否返回空集(通过测试的返回值),并将其移动光标到第一结果(当集不为空)。请注意,谨防空返回的集合,您发布的code应该测试返回值(它不是做)。
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()
,该测试如果(C!= 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中的游标使用moveToFirst()的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!