当查询是否在Android,则返回null? [英] When Does Query Returns Null on Android?

查看:183
本文介绍了当查询是否在Android,则返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎没有找到查询任何信息,插入或任何其它SQL方法返回null。但它确实如果发生错误。我只是想知道光标被空是否意味着发生错误或者可能意味着选择了(例如)没有行?我不知道我应该怎么对待它 - 作为一个错误,或者一些可能发生不时

I don't seem to find any information on query, insert or any other SQL method returning null. But it does if error occurs. I just wonder whether cursor being null means an error occured or could it mean no rows were selected (for instance)? I don't know how I should treat it - as an error or something that may happen time to time.

推荐答案

我不相信你需要检查如果(光标== NULL){}

I don't believe you ever need to check if(cursor == null) {}.

第一结果
如果您的查询不返回任何行,您将收到的的光标。将光标的的是

First
If your query doesn't return any rows, you will receive an empty Cursor. The Cursor will not be null.

有很多方法来检查光标是空的:

There are many ways to check if a Cursor is empty:


  • 如果(cursor.getCount == 0){}

  • 如果(!cursor.moveToFirst()){}

  • if(cursor.getCount == 0) {}
  • if(!cursor.moveToFirst()) {}

在事实上所有的光标#的moveTo的...()方法返回为真正,如果您收到然后您请求不存在该行。

In fact all of the Cursor#moveTo...() methods return either true or false, if you receive false then the row that you requested does not exist.

结果
如果再出现错误,你需要赶在try-catch块的错误,否则将应用程序从一个未处理的异常崩溃。

Second
If an error occurs then you need to catch the error in a try-catch block, otherwise the app will crash from an unhandled exception.

还有插入()更新()删除()返回一个整数,而不是一个游标。这些方法返回受你的语句行数,如果没有行受影响这些方法返回 0

Also insert(), update(), and delete() return an integer, not a Cursor. These methods return the number of rows affected by your statement, if no rows are affected these methods return 0.

这篇关于当查询是否在Android,则返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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