获取请求的异常索引50,大小为50

时间:2013-10-03 07:09:27

标签: java android database sqlite

从数据库中获取数据时出现异常。我想从这行代码column index 1

中获取String ques=c.getString(1);的值

这是我调用数据库类并尝试获取数据库的代码,

try{
   c=db.getText(Integer.toString(subid));
   if(c==null)
    return;

    //if(c.getCount()> 0)
    //{
    //int max=c.getCount();
    //int min=0;

    Random rn = new Random();       
    int randomNum =  rn.nextInt(max-1) + rowid;
    c.moveToPosition(randomNum);

    String ques=c.getString(1);
    tv.setText(ques+" ");

    cans=c.getString(2);
    shuffleArray(ans);
    int[] a = new int[4];
    int j=0;
        for (int i = 0; i < ans.length; i++)
        {
          a[j]=ans[i];
          j++;
        }
     ans1=c.getString(a[0]);
     ans2=c.getString(a[1]);
     ans3=c.getString(a[2]);
     ans4=c.getString(a[3]);
     rb1.setText(ans1);
     rb2.setText(ans2);
     rb3.setText(ans3);
     rb4.setText(ans4);
    }

   catch(Exception e)
{
    Toast.makeText(getApplicationContext(), e.getMessage()+"exception", Toast.LENGTH_LONG).show();
}

快照我的数据库内容,

enter image description here

这是我的数据库类,

public Cursor getText(String subId) throws SQLException //here it'll get all rows of subId=1 or whatever the value of subId
 {
  Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
        KEY_id, KEY_ques, KEY_correctans, KEY_wrongans1,KEY_wrongans2, KEY_wrongans3}, 
        KEY_subjectid + "=" +  " '" + subId + "' " , null,null,null, null,null);
if (mCursor != null) 
{
    mCursor.moveToFirst();
}
return mCursor;

}

正如我上面的详细信息和代码,当subId is 1的值然后它从subId 1的数据库中获取所有内容而没有任何异常,但是当我传递{{1}的值时然后它给出subId=2例外 当我通过CursorIndexOutOfBound: Index 50 requested, with a size of 50时,我得到subId=3 随机数生成器有什么问题吗?

实际上我无法解决这里的问题。

我需要帮助。

先谢谢。

2 个答案:

答案 0 :(得分:0)

尝试以这种方式生成随机数:(我假设c是光标)

int randomNum =  rowid + (rn.nextInt() % (c.getCount() - rowid));

希望这会有所帮助:)

答案 1 :(得分:0)

Use space in query " = " actually your column name become "KEY_subjectid=" which is wrong 




 KEY_subjectid + " =" +  " '" + subId + "' " , null,null,null, null,null);