0

I have populated a ListView from an SQLite database in Android. When I run the app list view does not fill with data - it is empty. I am getting an exception of DataBaseObjectNotClosedException even if I close the Cursor also. Can anyone see where my mistake is?

Here is my log file:

07-15 14:46:45.372: E/Database(18100): close() was never explicitly called on database '/data/data/com.sqlitedemo/databases/EmployeeReview.db' 
07-15 14:46:45.372: E/Database(18100): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-15 14:46:45.372: E/Database(18100):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
07-15 14:46:45.372: E/Database(18100):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
07-15 14:46:45.372: E/Database(18100):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
07-15 14:46:45.372: E/Database(18100):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
07-15 14:46:45.372: E/Database(18100):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
07-15 14:46:45.372: E/Database(18100):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-15 14:46:45.372: E/Database(18100):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
07-15 14:46:45.372: E/Database(18100):  at com.sqlitedemo.Employee_List.showList(Employee_List.java:148)
07-15 14:46:45.372: E/Database(18100):  at com.sqlitedemo.Employee_List.onCreate(Employee_List.java:63)
07-15 14:46:45.372: E/Database(18100):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 14:46:45.372: E/Database(18100):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-15 14:46:45.372: E/Database(18100):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-15 14:46:45.372: E/Database(18100):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-15 14:46:45.372: E/Database(18100):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-15 14:46:45.372: E/Database(18100):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 14:46:45.372: E/Database(18100):  at android.os.Looper.loop(Looper.java:123)
07-15 14:46:45.372: E/Database(18100):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 14:46:45.372: E/Database(18100):  at java.lang.reflect.Method.invokeNative(Native Method)
07-15 14:46:45.372: E/Database(18100):  at java.lang.reflect.Method.invoke(Method.java:507)
07-15 14:46:45.372: E/Database(18100):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 14:46:45.372: E/Database(18100):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 14:46:45.372: E/Database(18100):  at dalvik.system.NativeStart.main(Native Method)

Here is my method in Activity class

private void showList()
    {     

    ArrayList<Employee> EmployeeList = new ArrayList<Employee>();  
    EmployeeList.clear();  

    String strQuery = "SELECT sum(empid) as emp_id, Staff_emp_name as emp_name, department as emp_dept, designation as emp_designation,"
                    +" (SUM(performance_rate_one+performance_rate_two+performance_rate_three+performance_rate_four+performance_rate_five)/5) as TotalPerformance"
                    +" FROM employee_details INNER JOIN performance"
                    +" ON employee_details.Emp_id = performance.empid "
                    +" GROUP BY empid "
                    +" ORDER BY TotalPerformance DESC";


    SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
    Cursor c1 =sqlDatabase.rawQuery(strQuery, null);


    if (c1 != null && c1.getCount() != 0) 
    {   
        if (c1.moveToFirst())

        {   
            do
            {    
                Employee EmployeeListItems = new Employee();  
                EmployeeListItems.setEmployeeId(c1.getInt(c1.getColumnIndex("emp_id")));
                EmployeeListItems.setName(c1.getString(c1.getColumnIndex("emp_name")));
                EmployeeListItems.setDepartment(c1.getString(c1.getColumnIndex("emp_dept")));
                EmployeeListItems.setDesignation(c1.getString(c1.getColumnIndex("emp_designation")));

                EmployeeListItems.setPerformancerate(c1.getString(c1.getColumnIndex("TotalPerformance")));
                EmployeeList.add(EmployeeListItems);     

            } while (c1.moveToNext());   

    } 
        startManagingCursor(c1);
        sqlDatabase.close();
        c1.close();
    } 

        EmployeeList_Adapter contactListAdapter = new EmployeeList_Adapter(Employee_List.this, EmployeeList); 
        listEmployee.setAdapter(contactListAdapter);  
        contactListAdapter.notifyDataSetChanged();

    }

I'm getting error at the line

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
Cursor c1 =sqlDatabase.rawQuery(strQuery, null);
3
  • where hav u opened ur db??? Commented Jul 15, 2014 at 9:25
  • @Meghna : I have not opened database anywhere in this class , i have only read . Commented Jul 15, 2014 at 9:28
  • 1
    ok den first close cursor den database Commented Jul 15, 2014 at 9:30

3 Answers 3

2
 sqlDatabase.close();
 c1.close();

It will be -

 c1.close();
 sqlDatabase.close();
Sign up to request clarification or add additional context in comments.

Comments

0

You are opening a new database connection every time you try to show the list, and never closing it.

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();

You should only open the database connection once, when your application starts up, and re-use this connection throughout your app.

1 Comment

:I have close the data base c1.close();sqlDatabase.close(); .Still data is not showing in listview
0
  1. Why are you getting the writable database to just read the data ? Change it to get read only database API call.

  2. The below statements should be in finally. You should check if the database object is indeed open before closing it.

sqlDatabase.close();
c1.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.