Natto
Natto

Reputation: 223

close() was never explicitly called on database,android database not closed

Test.java :

 SQLiteDatabase db=openOrCreateDatabase("data",MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS location(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME VARCHAR(100),CBcode VARCHAR(100));");
    Cursor c1 = db.rawQuery("Select NAME from location", null);
    Cursor c2 = db.rawQuery("Select CBcode from location", null);
    if(c1==null || c2 == null)
    {
        Toast.makeText(getBaseContext(), "Database values empty, please refer Help option    befor you start using the app..", Toast.LENGTH_LONG).show();
    }
    else
    {
    c1.moveToFirst();
    c2.moveToFirst();
    int col1=c1.getColumnIndex("NAME");
           int col2=c2.getColumnIndex("CBcode");
            for(c1.moveToFirst(); c1.moveToLast(); c1.moveToNext())
            {
                    for(c2.moveToFirst(); c2.moveToLast(); c2.moveToNext())
                    {
                            if(loc_name == c1.getString(col1))
                            {
                              my_cb = c2.getString(col2);
                              c2.close();
                              c1.close();
                              Toast.makeText(getBaseContext(), "alarm has been set", Toast.LENGTH_LONG).show();
                              break;
                            }
                            else
                            {
                            //  Toast.makeText(getBaseContext(), "No such values in database, please read Help carefully before using lovisis!", Toast.LENGTH_LONG).show();
                            }
                    }
            }
    }
    db.close();

In my above code,i am trying to access the database and start a new activity when the string matches,but i am getting close was never explicitly called on database error. PLease help

Upvotes: 1

Views: 681

Answers (1)

user1733583
user1733583

Reputation:

Natto,

I've changed your code as follows, it worked for me. Please check and let me know.

                SQLiteDatabase db=openOrCreateDatabase("data",MODE_PRIVATE,null);
                db.execSQL("CREATE TABLE IF NOT EXISTS location(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME VARCHAR(100),CBcode VARCHAR(100));");
                Cursor c1 = db.rawQuery("Select NAME from location", null);
                Cursor c2 = db.rawQuery("Select CBcode from location", null);
                if(c1==null || c2 == null)
                {
                    Toast.makeText(getBaseContext(), "Database values empty, please refer Help option befor you start using the app..", Toast.LENGTH_LONG).show();
                }
                else
                {
                    c1.moveToFirst();
                    c2.moveToFirst();

                    int col1=c1.getColumnIndex("NAME");
                    int col2=c2.getColumnIndex("CBcode");

                    while(c1.moveToNext() && c2.moveToNext())
                    {
                            if(loc_name.equals(c1.getString(col1)))
                            {
                               my_cb = c2.getString(col2);
                               Toast.makeText(getBaseContext(), "alarm has been set", Toast.LENGTH_LONG).show();
                            }
                            else
                            {
                            //  Toast.makeText(getBaseContext(), "No such values in database, please read Help carefully before using lovisis!", Toast.LENGTH_LONG).show();
                            }
                    }
                }
                c2.close();
                c1.close();
                db.close();

Your mistakes are, 1. two for loops are expensive, use a while with && as shown. 2. Close c1 and c2 also.

Upvotes: 1

Related Questions