android
  • sqlite
  • 2012-07-27 92 views 2 likes 
    2

    數據庫類如何從android中的光標檢索對應於特定字段的數據?

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

    是我得到的錯誤是可創建cursor.getInt(cursor.getColumnIndex("createable")); cursorindexoutofbounds雖然光標是有一些價值還是它給這個錯誤! thanx提前

    +0

    在取數據之前,您需要先移動光標到第一條記錄。 – 2012-07-27 12:27:14

    回答

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

    不是:

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

    使用

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

    這可能會解決你的問題。

    +1

    thanxxx它的工作 – 2012-07-27 12:22:53

    +0

    如果它解決了你的問題,你應該接受這個答案:) – 2012-07-27 12:28:58

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

    }

    相關問題