1

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

Gyonder
  • 3,674
  • 7
  • 32
  • 49
Natto
  • 213
  • 1
  • 4
  • 17
  • u r closing db when string match , so close outside (i.e) after loop – Senthil Apr 11 '13 at 14:34
  • Have you got any werror before close() was never explicitly called on database,android database not closed ? – Gyonder Apr 11 '13 at 14:39
  • 2
    These loops `for(c1.moveToFirst(); c1.moveToLast(); c1.moveToNext())` only check the last row of the Cursor because `moveToLast()` moves the Cursor, perhaps you meant to use `isAfterLast()` (or just `while(cursor.moveToNext())`)? – Sam Apr 11 '13 at 14:42
  • As for the error, the general rule is for each call to `getReadableDatabase()`, `getWritableDatabase()`, or `openOrCreateDatabase()` you must have a matching call to `close()`. – Sam Apr 11 '13 at 14:45
  • Last observation: you cannot compare Strings with `==` in Java, you _must_ use `equals()`. Please read [How do I compare strings in Java?](http://stackoverflow.com/q/513832/1267661) – Sam Apr 11 '13 at 14:48

1 Answers1

1

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.