What will a SQLiteCursor do if a column is null?

╄→гoц情女王★ 提交于 2020-01-03 06:44:08

问题


I want to get an integer from a Cursor returned from a SQLite query, but I know the integer may be null. Unfortunately I can't find any Cursor method allowing me to check this.

The code will be

mModifiedDate = cursor.getInt(cursor.getColumnIndex(MODIFIED_DATE));

I would expect a possible null value, and this is in fact desirable for various reasons—the field refers to the time a second table was modified, and the first table can be populated before the second one is. Unfortunately, the documentation for Cursor says that whether an exception is thrown, or an error value is returned, or other behaviour, is left up to the implementation, and the SQLiteCursor documentation doesn't say ANYTHING.

What will this code do if the field is null? Is there a way to check this before calling getInt()?


回答1:


there is an isNull()-method, just use if beforehand to detect null-values. Remember: It is quite common for methods that return a boolean to be named isX() instead of getX(), it can be the source of some confusion :)




回答2:


From my experience, it returns 0. If 0 isn't a possible answer, then you can just check for 0. If it is a possible answer, then you need to do as pgsandstrom suggested.



来源:https://stackoverflow.com/questions/7753274/what-will-a-sqlitecursor-do-if-a-column-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!