【问题标题】:Cursor Index Out of Bound光标索引超出范围
【发布时间】:2013-06-10 08:40:01
【问题描述】:

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

【问题讨论】:

    标签: android sqlite cursor


    【解决方案1】:

    试试这个...

    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;
    }
    

    【讨论】:

      【解决方案2】:
      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;
          }
      

      【讨论】:

        猜你喜欢
        • 2017-06-05
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 1970-01-01
        相关资源
        最近更新 更多