Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:50:54+00:00 2026-06-03T14:50:54+00:00

I am trying to get some data from my database. This is my code

  • 0

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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:50:56+00:00Added an answer on June 3, 2026 at 2:50 pm

    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.

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

    versus

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

    i.e the second updateData call in the first one gets called regardless, without the else if or 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some form data from POST method. Here's the code
I'm trying to get some data from my database, and then pass it into
I am trying to get some data from a database on the fly and
I am trying to make one query, to get some statistic data from database.
i'm trying to insert some data on a database using this code: -(void)insertLocationOnDatabase:(LocationType *)aLocation
I'm trying to get some data from a table, where the tag has no
I have sugar crm instance and i was trying to get some data from
I'm having some problems trying to get the code below to output the data
So this sucks. Trying to populate a core data database with info from NSDictionaries.
I am trying to make some data from database available using XML, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.