【问题标题】:What could be the cause of "java.lang.IllegalStateException: get field slot from row 0 col 0 failed"“java.lang.IllegalStateException: get field slot from row 0 col 0 failed”的原因可能是什么
【发布时间】:2011-08-30 19:42:28
【问题描述】:

我有一个 SQL 查询在“sqlite”数据库上运行,当我调用时抛出此异常:“java.lang.IllegalStateException: get field slot from row 0 col 0 failed”:

db.rawQuery( "SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) } );
if (!cursor.moveToFirst()) return;
bytes[] bytes = cursor.getBlob(0);

该列存在,如果我使用adb shell sqlite3 /path/to/database.db 登录,我可以成功执行相同的查询。

结果游标显示选择了 1 行,其中 1 列名称为“数据”。 blob 的大小约为 1.5 MB - 可能是这样吗?我已经验证cursor.GetColumnIndex("data")返回0,所以该列存在。

这是在 Android 2.1 SDK 上运行的。有什么想法吗?

【问题讨论】:

    标签: android sql exception sqlite


    【解决方案1】:

    问题在于 blob 太大。如果 blob 大小超过一兆字节,cursor.getBlob(0) 似乎会失败。分段读取 blob 解决了我的问题:

    // get length of blob data
    Cursor cursor = db.rawQuery( "SELECT LENGTH(data) AS len FROM table WHERE id = ?", etc);
    if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
    int size = cursor.getInt(cursor.getColumnIndexOrThrow("len"));
    cursor.close();
    
    // read a segment of the blob data
    cursor = db.rawQuery( "SELECT SUBSTR(data,0,500000) AS partial FROM table WHERE id = ?", etc);
    if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
    byte[] partial = cursor.getBlob(cursor.getColumnIndexOrThrow("partial"));
    cursor.close();
    
    // copy partial bytes, repeat until the whole length has been read...
    

    【讨论】:

      【解决方案2】:

      我假设您持有来自rawQuery 的光标引用。 在从光标获取任何值之前,您需要调用cursor.moveToFirst(),因为cursor 最初位于-1。正确的做法是:

      Cursor cursor = db.rawQuery( "SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) } );
      if(cursor!= null && cursor.moveToFirst()){ // checking if the cursor has any rows
         bytes[] bytes = cursor.getBlob(0);
      }
      

      还要确保您在数据库中的列是blob 类型

      【讨论】:

      • 我实际上正在这样做 - 我已经更新了问题。谢谢。
      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 2010-12-21
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多