I am trying to get some data from my database.
This is my code for it:
}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();
}
This is inside a context Menu, And updateData is an object referring to my SQLite class.
For some reason, only this else if() block of code is messing with my project.
When its there, the error console tells me that i cant do updateData.open() Because of this:
close() was never explicitly called on database......
But when i remove my else if block of code i dont get this error. What is wrong here?
Here is my full context Menu code:
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;
}
And here is my SQL class:
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;
}
}
Reformatted you code so i could see who was doing what to whom, not sure what the error is because I don’t do this stuff but the difference would be.
versus
i.e the second updateData call in the first one gets called regardless, without the
else ifor with some extra braces, only gets called if it’s true.Have a think about how you indent your code, it’s not just readability.