我的代码根据某些条件为某些表生成动态列。 该列可能存在也可能不存在。
所以我在做-
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/