【问题标题】:Cursor#getType() is Index Out Of BoundsCursor#getType() 是索引超出范围
【发布时间】:2013-04-27 03:36:41
【问题描述】:

基本上我有这个:

Cursor cur = ...
for (int i = 0; i < cur.getColumnCount(); i++) {
     String name = cur.getColumnName(i);
     Log.d("dao",name);
     int type = cur.getType(i);

... 并进入getType() 调用上述异常。 列名记录正确。

ERROR AndroidRuntime 原因: android.database.CursorIndexOutOfBoundsException:请求索引 -1, 大小为 0

【问题讨论】:

标签: android android-sqlite


【解决方案1】:

这是 SQLite 数据库的一个特性,它使用动态类型。也检查this。 你为什么不使用:

Cursor cur = ...

if (cursor != null) {
    while (cursor.moveToNext()) {
        String name = cur.getColumnName(i);
        int type = cur.getType(i);
        ...
    }
}

这样你只询问类型,如果存在一行。

如果您需要独立于任何结果的列类型,您可以使用:

String tableName = "...";
Cursor cursor = rawQuery("pragma table_info(" +tableName +")");
String type = getString(0);
String name = getString(1);

希望这会有所帮助……干杯!

【讨论】:

  • 我确实做过测试,但我没有发布。我的光标测试中有一个错误。无论如何,谢谢你的解释。
猜你喜欢
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多