1. Check out our app, Forums for Android! Download and leave feedback here!

"Database not open" Error

Discussion in 'Application Development' started by j_p36, Jun 30, 2010.

  1. j_p36

    j_p36 Member
    Thread Starter
    15

    May 19, 2010
    16
    0
    15
    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 (Text):
    1. db = new DbAdapter(SyncService.this);
    2.            
    3.             showNotification("Sync Running");
    4.            
    5.             db.open();
    6.             Map<String, Cursor> results = db.updateList(communityGuid);
    7.             Cursor newInfo = results.get("newInfo");
    8.             String infoEnv = "";
    9.             if(newInfo.moveToFirst())
    10.             {
    11.                 ArrayList<String> infos = new ArrayList<String>();
    12.                 infoEnv = AddInfoPrefix();
    13.                 while(!newInfo.isAfterLast())
    14.                 {
    15.                     infos.add(newInfo.getString(6));
    16.                     infoEnv+=AddInfo(newInfo.getString(0), newInfo.getString(1), newInfo.getString(2), newInfo.getString(3), newInfo.getString(4), newInfo.getString(5), newInfo.getString(6));
    17.                     newInfo.moveToNext();
    18.                 }
    19.                 infoEnv+=AddInfoSuffix();
    20.                 soapAction = buildSoapAction("AddInfo");
    21.                 if(!httpAndParse("addInfo", infoEnv, soapAction, communityGuid, ip))
    22.                 {
    23.                     successful = false;
    24.                 }
    25.                 else
    26.                 {
    27.                     db.infoSent(infos);
    28.                 }
    29.             }
    30.             newInfo.close();
    31.             db.close();
    the DbAdapater code
    Code (Text):
    1. public boolean infoSent(ArrayList<String> ids)
    2.         {
    3.             boolean fail=false;
    4.             ContentValues Con = new ContentValues();
    5.             Con.put(KEY_NEW, 0);
    6.             for(String id : ids)
    7.             {
    8.                 if(mDb.update(DATABASE_TABLE2, Con, KEY_ID + "=?", new String[]{id})<=0)
    9.                 {
    10.                     fail=true;
    11.                 }
    12.             }
    13.             return !fail;
    14.         }
    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?
     

    Advertisement

  2. cp1

    cp1 Well-Known Member
    53

    Apr 30, 2010
    375
    35
    53
    so UpdateList works? What is that code? Is the problem with the database name? (DATABASE_TABLE2)?
     
  3. j_p36

    j_p36 Member
    Thread Starter
    15

    May 19, 2010
    16
    0
    15
    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 (Text):
    1.  
    2. public Map<String, Cursor> updateList(String communityGuid)
    3.         {
    4.             Map<String, Cursor> OverallResults = new HashMap<String, Cursor>();
    5.             ...
    6.             Cursor curs3 = mDb.rawQuery("SELECT p.i_guid, p.caption, p.utc, p.uuid " +
    7.                     "FROM picvid p, info i, folders f " +
    8.                     "WHERE f.cuuid=? and i.f_guid=f.uuid and p.i_guid=i.uuid and p.new=1", new String[]{communityGuid});
    9.             OverallResults.put("picvid", curs3);
    10.             ...
    11.             return OverallResults;
    12.         }
    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
     
  4. cp1

    cp1 Well-Known Member
    53

    Apr 30, 2010
    375
    35
    53
    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).
     

Share This Page

Loading...