我试图在数据库中搜索ID是否存在,当它存在时,我打开下一个活动,但当它不存在时,应用程序崩溃。我找不到错误,错误光标索引越界

                 Database dbc = new Database(this);
                dbc.open();
                String id = searchId.getText().toString();
                boolean checkId = dbc.isGotId(id);

                if(checkId == true){
                    String s = searchId.getText().toString();
                    Bundle b = new Bundle();
                    b.putString("key",s);
                    b.putInt("keyX", radioBtnFlag);
                    Intent a = new Intent(SearchUpdate.this, UpdateDelete.class);
                    a.putExtras(b);
                    startActivity(a);

                    searchId.setText(null);

                }else if(checkId == false){

                    Log.v(id, id + "2222222");

                    Dialog d = new Dialog(this);
                    d.setTitle("Error!");
                    TextView tv = new TextView(this);
                    tv.setText("This Search is allow only ID! "+ radioBtnFlag);
                    d.setContentView(tv);
                    d.show();

                    searchId.setText(null);

在这里…
  public boolean isGotId(String id){

    boolean result = false;
    try{

    Cursor sId = ourDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + Pro_ID + "=" + id, null);

    result = true;
    }catch(SQLiteException e)
    {
        result = false;
    }//catch

    return result;

}//isGOtId

最佳答案

试试这个…

public boolean isGotId(String id){

        Cursor sId = ourDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
                                  Pro_ID + "=" + id, null);
        int numberOfRows = sId.getCount();
        if(numberOfRows <= 0)
        {
            return false;
        }

    return true;
}

关于android - 游标索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17019902/

10-12 02:15