gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:09:36 25 4
gpt4 key购买 nike

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

所以我在做-

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com