2

数据库类

public boolean Permissions(String modulename) {
    int createable, updateable;
    Cursor cursor = db.rawQuery("select * from " + "moduleDesc" + " where "
            + "name" + "='" + modulename + "'", null);
    if (cursor.getCount() > 0);
        {
        createable = cursor.getInt(cursor.getColumnIndex("createable"));
        updateable = cursor.getInt(cursor.getColumnIndex("updateable"));
        cursor.close();
    }
    if ((createable == 1) && (updateable == 1)) {
        return true;

    } else {
        return false;
    }
}

我得到的错误是可创建的 cursorindexoutofbounds cursor.getInt(cursor.getColumnIndex("createable")); 尽管光标有一些价值,但它仍然给出了这个错误!!!提前谢谢

4

3 回答 3

1

而不是:

  if (cursor.getCount() > 0) { 
      //Code here
  }

采用

  if (cursor.moveToFirst()) { 
      //Code here
  }

这可能会解决您的问题。

于 2012-07-27T12:17:53.177 回答
1
Try this solution 

public boolean Permissions(String modulename) {
int createable, updateable;
Cursor cursor = db.rawQuery("select * from " + "moduleDesc" + " where "
        + "name" + "='" + modulename + "'", null);
if (cursor.getCount() > 0);
    {
    cursor.MoveToFirst();//use this if cursor count is 1 else use while loop 
    //while (cursor.MoveToNext){
    createable = cursor.getInt(cursor.getColumnIndex("createable"));
    updateable = cursor.getInt(cursor.getColumnIndex("updateable"));
    //}
    cursor.close();
}
if ((createable == 1) && (updateable == 1)) {
    return true;

} else {
    return false;
}

}

于 2012-07-27T12:19:56.617 回答
0

正确关闭光标。

public boolean Permissions(String modulename) {
    Cursor cursor = db.rawQuery("select * from " + "moduleDesc" + " where "
            + "name" + "='" + modulename + "'", null);
    if (cursor != null) {
        try {
           if (cursor.moveToFirst()) {
               int createable = cursor.getInt(cursor.getColumnIndex("createable"));
               int updateable = cursor.getInt(cursor.getColumnIndex("updateable"));
               return (createable == 1) && (updateable == 1);
           }
        } finally {
            cursor.close();
        }
    }
   return false;
}
于 2012-07-27T12:22:07.473 回答