android - Sqlite 中不存在列的返回类型?

标签 android sqlite android-cursor

我的代码根据某些条件为某些表生成动态列。 该列可能存在也可能不存在。

所以我在做-

cur.getString(cur.getColumnIndex("Dynamic_Column_Name"))

cur.getColumnIndex("Dynamic_Column_Name") 的 API 说 -

Returns the zero-based index for the given column name, or -1 if the column doesn't exist.

因此,如果 Dynamic_Column_Name 不是我表中的列,那么它将是--

cur.getString(-1)

cur.getString(-1) 的 api 说 -

Returns the value of the requested column as a String. The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.

那么在我的情况下返回类型是什么,或者它会通过一些异常吗?

有什么最好的方法来处理这种情况吗?

最佳答案

你会得到一个异常(exception)。具体来说:

java.lang.IllegalStateException: Couldn't read row n, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

为避免这种情况,请在使用前检查列索引是否为-1。

关于android - Sqlite 中不存在列的返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23359956/

相关文章:

java - 在没有游标的情况下访问 Android Sqlite 中的大 BLOB

java - 永久更改gridview android中的按钮背景图像

android - SQLite SELECT MAX() 查询向游标返回空值?

java - PreparedStatement 上的 ArrayOutOfBoundsException

java - 由 : android. database.sqlite.SQLiteException 引起:接近 "":语法错误(代码 1):

Android - 从特定 Thread_ID 查询所有彩信和短信

java - 如何在android中为一项 Activity 设置屏幕旋转?

javascript - 为什么 cordova-local-notification-plugin 不起作用(Phonegap、Android)?

android - Jenkins:Android 项目构建失败(生成数据绑定(bind)/ Dagger 类失败)

Android Cursor - moveToNext() 似乎没有到达光标的末尾