• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps "Database not open" Error

j_p36

Newbie
May 19, 2010
16
0
When I run the below code I get:

java.lang.IllegalStateException: database not open

I don't understand this because I clearly see db.open() above my code. Any help with this would be greatly appreciated.

Code:
db = new DbAdapter(SyncService.this);
            
            showNotification("Sync Running");
            
            db.open();
            Map<String, Cursor> results = db.updateList(communityGuid);
            Cursor newInfo = results.get("newInfo");
            String infoEnv = "";
            if(newInfo.moveToFirst())
            {
                ArrayList<String> infos = new ArrayList<String>();
                infoEnv = AddInfoPrefix();
                while(!newInfo.isAfterLast())
                {
                    infos.add(newInfo.getString(6));
                    infoEnv+=AddInfo(newInfo.getString(0), newInfo.getString(1), newInfo.getString(2), newInfo.getString(3), newInfo.getString(4), newInfo.getString(5), newInfo.getString(6));
                    newInfo.moveToNext();
                }
                infoEnv+=AddInfoSuffix();
                soapAction = buildSoapAction("AddInfo");
                if(!httpAndParse("addInfo", infoEnv, soapAction, communityGuid, ip))
                {
                    successful = false;
                }
                else
                {
                    db.infoSent(infos);
                }
            }
            newInfo.close();
            db.close();
the DbAdapater code
Code:
public boolean infoSent(ArrayList<String> ids)
        {
            boolean fail=false;
            ContentValues Con = new ContentValues();
            Con.put(KEY_NEW, 0);
            for(String id : ids)
            {
                if(mDb.update(DATABASE_TABLE2, Con, KEY_ID + "=?", new String[]{id})<=0)
                {
                    fail=true;
                }
            }
            return !fail;
        }
it fails once db.infoSent is called and it reaches the "if(mDb.update" line

does it have something to do with it being in the else statement? or with the cursors that are sitting in the map?

I got the program to run right by rearranging some stuff, but I still have to open the database right before my call to infoSent... why would this be?
 
Yes, updateList works beautifully, and the name DATABASE_TABLE2 is a static variable set to the database name and it works fine. My code uses DATABASE_TABLE2 successfully in many other places.

Code:
public Map<String, Cursor> updateList(String communityGuid)
        {
            Map<String, Cursor> OverallResults = new HashMap<String, Cursor>();
            ...
            Cursor curs3 = mDb.rawQuery("SELECT p.i_guid, p.caption, p.utc, p.uuid " +
                    "FROM picvid p, info i, folders f " +
                    "WHERE f.cuuid=? and i.f_guid=f.uuid and p.i_guid=i.uuid and p.new=1", new String[]{communityGuid});
            OverallResults.put("picvid", curs3);
            ...
            return OverallResults;
        }

what I don't understand is why I call db.open() before I declare the Map<String, Cursor>, and I don't call close until much later. But to make the code work I have to call db.open() again right before I call db.infoSent() and then db.close() right after it
 
Upvote 0
Come to think of it I had a similar problem with calling open() and then using the database a while later. I ended up calling open() and close() just before and after every time I used the database -- I'm not sure what the problem was, but that fixed it and stopped the memory leak I was getting (database wasn't closing properly sometimes).
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones