2012-05-10 128 views
0

我想從我的數據庫中獲取一些數據。 這是我爲它的代碼:Android SQLite奇怪的錯誤?

}else if(menuItemName=="Toggle Reset Protection"){ 
    int is = updateData.getSingleProtect(listItemName, listTotal); 
    int val = 0; 
    if(is==0) 
    { 
     val = 1; 
    } 
    else 
     if(is==1) 
     { 
     val = 0; 
     } 
    updateData.protect(listItemName, listTotal, val); 
    onResume(); 
    } 

這是一個上下文菜單裏面,是了updateData指我的SQLite的類的對象。 由於某種原因,只有這else if()代碼塊搞亂了我的項目。

當它的存在,錯誤控制檯告訴我,我不能做updateData.open()正因爲如此:

close() was never explicitly called on database...... 

但是,當我刪除我的代碼else if塊我沒有得到這個錯誤。這裏有什麼問題?

這裏是我完整的上下文菜單代碼:

public boolean onContextItemSelected(MenuItem item) { 
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
int menuItemIndex = item.getItemId(); 
String[] menuItems = new String[]{"Delete", "Edit", "Toggle Reset Protection", "Add"}; 
String menuItemName = menuItems[menuItemIndex]; 
final String listItemName = name[info.position]; 
final int listCurrent = current[info.position]; 
final int CurrentTarget = listCurrent+1; 
final int listTotal = total[info.position]; 
SetSql updateData = new SetSql(SpellCast.this); 
updateData.open(); 

if(menuItemName=="Delete"){ 
    updateData.delete(listItemName, listTotal); 
    onResume(); 
}else if(menuItemName=="Add"){ 
    if(listCurrent<listTotal){ 
    updateData.changeCurrent(CurrentTarget, listCurrent, listItemName); 
    onResume(); 
    } 
    }else if(menuItemName=="Edit"){ 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle(listItemName); 
    alert.setMessage("Enter new maximum value:"); 
    final EditText input = new EditText(this); 
    alert.setView(input); 
    input.setText(Integer.toString(listTotal)); 
    input.setFilters(new InputFilter[] { 
      // Maximum 2 characters. 
      new InputFilter.LengthFilter(3), 
      // Digits only. 
      DigitsKeyListener.getInstance(), // Not strictly needed, IMHO. 
     }); 

     // Digits only & use numeric soft-keyboard. 
    input.setKeyListener(DigitsKeyListener.getInstance()); 
    alert.setPositiveButton("Change", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        String totalString = input.getText().toString(); 
        int totalv = Integer.parseInt(totalString); 
        changeTotal(totalv, listTotal, listItemName); 
        changeCurrent(totalv, listCurrent, listItemName); 
        onResume(); 
       } 
      }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      // do nothing 
     } 
    }); 
    alert.show(); 
    }else if(menuItemName=="Toggle Reset Protection"){ 

    int is = updateData.getSingleProtect(listItemName, listTotal); 
    int val = 0; 

    if(is==0){ 
     val = 1; 
    }else if(is==1){ 
    val = 0; 
    } 
    updateData.protect(listItemName, listTotal, val); 
    onResume(); 
    } 
    updateData.close(); 
    return true; 
} 

,這裏是我的SQL類:

private static class DbHelper extends SQLiteOpenHelper{ 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_CASTING + " (" + 
       KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_NAME + " VARCHAR NOT NULL, " + 
       KEY_TOTAL + " INTEGER NOT NULL, " + 
       KEY_CURRENT + " INTEGER NOT NULL, " + 
       KEY_PROTECT + " INTEGER NOT NULL);" 
       ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CASTING); 
     onCreate(db); 
    } 


} 

public SetSql(Context c){ 
    ourContext = c; 
} 

public SetSql open(){ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

public void close(){ 
    ourHelper.close(); 
} 

public void createEntry(String name, int total, int protect) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_TOTAL, total); 
    cv.put(KEY_CURRENT, total); 
    cv.put(KEY_PROTECT, protect); 
    ourDatabase.insert(DATABASE_CASTING, null, cv); 
} 
public void changeCurrent(int value, int cval, String name){ 
    ourDatabase.execSQL("UPDATE " + DATABASE_CASTING + " SET " + KEY_CURRENT + " = '" + value + "' WHERE " + KEY_NAME + " = '" + name + "' AND " + KEY_CURRENT + " = '" + cval + "'"); 
} 

public void changeTotal(int newtotal, int oldtotal, String name){ 
    ourDatabase.execSQL("UPDATE " + DATABASE_CASTING + " SET " + KEY_TOTAL + " = '" + newtotal + "' WHERE " + KEY_NAME + " = '" + name + "' AND " + KEY_TOTAL + " = '" + oldtotal + "'"); 
} 

public String[] getNames() throws SQLException{ 
    String[] columns = new String[]{KEY_NAME}; 
    Cursor c = ourDatabase.query(DATABASE_CASTING, columns, null, null, null, null, KEY_ROWID); 
    String[] result = new String[100]; 

    int iName = c.getColumnIndex(KEY_NAME); 
    //might cause errors here below... 
    int count = 0; 
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result[count] = c.getString(iName); 
     count++; 
    } 
    count = 0; 
    return result; 
} 

public int[] getTotal(){ 
    int[] totals = new int[100]; 
    String[] columns = new String[]{KEY_TOTAL}; 
    Cursor c = ourDatabase.query(DATABASE_CASTING, columns, null, null, null, null, KEY_ROWID); 
    int iTotal = c.getColumnIndex(KEY_TOTAL); 

    int count = 0; 
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     totals[count] = c.getInt(iTotal); 
     count++; 
    } 
    count = 0; 
    return totals; 
} 
public int[] getCurrent(){ 
    int[] currents = new int[100]; 
    String[] columns = new String[]{KEY_CURRENT}; 
    Cursor c = ourDatabase.query(DATABASE_CASTING, columns, null, null, null, null, KEY_ROWID); 
    int iCurrent = c.getColumnIndex(KEY_CURRENT); 

    int count = 0; 
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     currents[count] = c.getInt(iCurrent); 
     count++; 
    } 
    count = 0; 
    return currents; 
} 
public int enteries(){ 
    int num = 0; 
    String[] columns = new String[]{KEY_ROWID}; 
    Cursor c = ourDatabase.query(DATABASE_CASTING, columns, null, null, null, null, null); 
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     num++; 
    } 
    return num; 
} 

public void delete(String name, int total) { 
    // TODO Auto-generated method stub 
    ourDatabase.execSQL("DELETE FROM " + DATABASE_CASTING + " WHERE " + KEY_NAME + " = '" + name + "' AND " + KEY_TOTAL + " = '" + total + "'"); 
} 

public int[] getProtect(){ 
    int[] protect = new int[100]; 


    String q = "SELECT " + KEY_PROTECT + " FROM " + DATABASE_CASTING + " ORDER BY " + KEY_ROWID + ";"; 
    Cursor mCursor = ourDatabase.rawQuery(q, null); 

    int iProt = mCursor.getColumnIndex(KEY_PROTECT); 

    int count = 0; 
    for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()){ 
     protect[count] = Integer.parseInt(mCursor.getString(iProt)); 
     count++; 
    } 
    count = 0; 

    return protect; 
} 

public void protect(String name, int total, int value){ 
    ourDatabase.execSQL("UPDATE " + DATABASE_CASTING + " SET " + KEY_PROTECT + " = '" + value + "' WHERE " + KEY_NAME + " = '" + name + "' AND " + KEY_TOTAL + " = '" + total + "';"); 
} 

public int getSingleProtect(String name, int total){ 
    String q = "SELECT * FROM " + DATABASE_CASTING + " WHERE " + KEY_NAME + " = '" + name +"' AND " + KEY_TOTAL + " = '" + total + "';"; 
    Cursor mCursor = ourDatabase.rawQuery(q, null); 

    int index = mCursor.getColumnIndex(KEY_PROTECT); 
    int prot = mCursor.getInt(index); 

    return prot; 
} 
} 
+0

什麼是updateData.open()?你談論的通常是當db.close()沒有被調用,其中db是SQLiteDatabase。 –

+0

updateData.open();是一個打開數據庫和dbHelper的函數。 updateData.close();關閉它們。 – arielschon12

+0

你能告訴我們哪個'else if'是什麼意思? (有兩個...)你也可以告訴我們什麼'updateData.protect()','getSingleProtect()'和'onResume()'並且在哪個上下文中調用它?基本上確保在所有'if'情況下,在完成後顯式關閉數據庫...... – WarrenFaith

回答

0

重新格式化您的代碼,所以我可以看到誰在做什麼給誰,不知道是什麼錯誤是因爲我不做這個東西,但差異會。

else 
     if(is==1) 
     { 
     val = 0; 
     } 
    updateData.protect(listItemName, listTotal, val); 
    onResume(); 

else 
    { 
     if(is==1) 
     { 
     val = 0; 
     } 
     updateData.protect(listItemName, listTotal, val); 
     onResume(); 
    } 

即在第一個無論被稱爲第二了updateData呼叫,而不else if或一些額外的支撐,如果這是真的只有被調用。

想想你如何縮進你的代碼,這不僅僅是可讀性。

+0

哎呦,複製粘貼中的東西出錯了,再看看我的主線程 – arielschon12

+0

我修好了!看看我的主線程 – arielschon12

+0

看不到任何東西,並看到你已經修復它,沒有感覺到一個強大的需要重新縮進你的代碼。如果是的話,永遠不會理解這種注視。如果{else} {if {}},所以你不能看到誰在做什麼是我的選擇。 –