2014-07-15 55 views
0

我已經從Android中的SQLite數據庫填充了一個ListView。當我運行應用程序列表視圖不填充數據 - 它是空的。即使我還關閉Cursor,我也會收到DataBaseObjectNotClosedException的例外情況。任何人都可以看到我的錯誤在哪裏?Android中的Sqlite

這裏是我的日誌文件:

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); 
+0

其中甲肝ü打開烏爾分貝??? – Meghna

+0

@Meghna:我沒有在這堂課的任何地方打開數據庫,我只讀過。 – jvd

+1

ok den first first cursor den database – Meghna

回答

0

您收到錯誤每次嘗試顯示l時打開一個新的數據庫連接ist,永遠不會關閉它。

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase(); 

您應該只打開數據庫連接一次,當應用程序啓動和再利用整個應用此連接。

+0

:我已關閉數據庫c1.close(); sqlDatabase.close(); 。仍然沒有在列表視圖中顯示數據 – jvd

0
  1. 爲什麼要讓可寫數據庫讀取數據?將其更改爲只讀數據庫API調用。

  2. 以下語句應該在最後。在關閉它之前,你應該檢查數據庫對象是否確實打開。

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

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

這將是 -

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