2

我试图在数据库中搜索 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
4

2 回答 2

4

尝试这个...

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;
}
于 2013-06-10T08:51:11.027 回答
3
try{

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

  if(sId.moveToFirst() && sId ! = null)
             result = true;
    }catch(SQLiteException e)
    {
        result = false;
    }
于 2013-06-10T08:47:11.330 回答