Android中的Sqlite:DataBaseObjectNotClosedException

时间:2014-07-15 09:23:25

标签: java android android-sqlite

我已在Android中的ListView数据库中填充了SQLite。当我运行应用程序列表视图时没有填充数据 - 它是空的。即使我关闭DataBaseObjectNotClosedException,我也会获得Cursor的例外情况。任何人都可以看到我的错误在哪里吗?

这是我的日志文件:

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)

这是我在Activity类

中的方法
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();

    }

我在第

行收到错误
SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
Cursor c1 =sqlDatabase.rawQuery(strQuery, null);

3 个答案:

答案 0 :(得分:2)

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

会是 -

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

答案 1 :(得分:0)

每次尝试显示列表时都会打开一个新的数据库连接,并且永远不会关闭它。

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();

您应该只在应用程序启动时打开一次数据库连接,并在整个应用程序中重复使用此连接。

答案 2 :(得分:0)

  1. 为什么要让可写数据库只读取数据?将其更改为只读数据库API调用。

  2. 以下陈述应该在最后。在关闭之前,您应该检查数据库对象是否确实打开。

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