【问题标题】:My application is throwing cursor index outOfBounds exception,requested -1 with size of 1我的应用程序抛出光标索引 outOfBounds 异常,请求大小为 1 的 -1
【发布时间】:2019-05-24 06:28:27
【问题描述】:

当我尝试从我的 sqlite 数据库中检索 blob 时,游标抛出 index outofbounds 异常。

我的 DataBaseHelper 类是:

public class DataBaseHelper extends SQLiteOpenHelper {
    public static final String Database_Name="images.db";
    public static final String Table_Name="imgs";
    Bitmap bitmap;
    public DataBaseHelper(Context context) {
        super(context, Database_Name,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+Table_Name+"(id integer primary key autoincrement , image blob)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("drop table if esists "+Table_Name);
    }
    public boolean insert(byte[] img)
    {

        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("image",img);
        long insert=db.insert(Table_Name,null,cv);
        if(insert==-1)
        {
            return  false;
        }
        else
        {
            return true;
        }
    }

还有我的结果类:

public class Results extends AppCompatActivity {
    ImageView imageView;
    DataBaseHelper mDataBaseHelper;
    Bitmap img;
    Bitmap bitmap;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        textView=(TextView)findViewById(R.id.textView3);
        mDataBaseHelper=new DataBaseHelper(this);
        SQLiteDatabase db=mDataBaseHelper.getReadableDatabase();
        Toast.makeText(getApplicationContext(),"Emm",Toast.LENGTH_LONG).show();
        Cursor cursor=db.rawQuery("select* from "+mDataBaseHelper.Table_Name,null);
        byte[] imageArray=cursor.getBlob(3);
        bitmap=BitmapFactory.decodeByteArray(imageArray,0,imageArray.length);
        imageView.setImageBitmap(bitmap);
    }
}

上面的结果代码正在抛出cursor index outofbounds exception,index -1 requested with size of 1

【问题讨论】:

    标签: android cursor blob


    【解决方案1】:

    if (cursor.moveToNext()){ cursor.getBlob(cursor.getColumnIndexOrThrow(yourColumnName)); }

    通过这种方式,您将光标移动到返回数据集的第一行。 无论如何,您的 select 语句中似乎遗漏了一个空格。

    【讨论】:

      【解决方案2】:

      您要从中获取行的表mDataBaseHelper.Table_Name
      通过执行此语句:

      "select * from " + mDataBaseHelper.Table_Name
      

      只有 1 列,但您尝试通过以下方式获得第 4 列:

      byte[] imageArray=cursor.getBlob(3);
      

      SQLiteOpenHelper类中的CREATE语句显示表中有2列:idimage,所以你应该这样做:

      if (cursor.moveToFirst()) {
          byte[] imageArray=cursor.getBlob(1);
      }
      cursor.close();
      

      尝试一下,但如果再次遇到同样的错误,这意味着数据库中存在的表只有 1 列。
      因此,如果问题仍然存在,请从模拟器/设备中卸载应用程序,以便删除数据库并再次运行以重新创建表。然后重新填充表格。

      【讨论】:

      • 如果您尝试getBlob(1) 并得到indexoutofbounds 错误,那么您的表格只有一列!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多