2014-01-08 62 views
0

以下是我的代码,无法关闭数据库正确

public class CommentsDataSource { 

     private SQLiteDatabase database; 
     private MySQLiteHelper dbHelper; 
     private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
       MySQLiteHelper.COLUMN_COMMENT }; 

     public CommentsDataSource(Context context) { 
      dbHelper = new MySQLiteHelper(context); 
     } 

     public void open() throws SQLException { 
      database = dbHelper.getWritableDatabase(); 
     } 

     public void close() { 
      if (database != null) { 
       database.close(); 

      } 
      dbHelper.close(); 

     } 

     public String getComment_1() { 
      List<Comment> comments = new ArrayList<Comment>(); 

      Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
        allColumns, null, null, null, null, null); 

      cursor.moveToFirst(); 
      while (!cursor.isAfterLast()) { 
       Comment comment = cursorToComment(cursor); 
       comments.add(comment); 
       cursor.moveToNext(); 
      } 
      // make sure to close the cursor 
      cursor.close(); 

      return comments.get(0).getComment(); 
     } 

     private Comment cursorToComment(Cursor cursor) { 
      Comment comment = new Comment(); 
      comment.setId(cursor.getLong(0)); 
      comment.setComment(cursor.getString(1)); 

      return comment; 
     } 
public class MyService extends BackgroundService implements LocationListener { 
@Override 
    public void onCreate() { 

     context = getApplicationContext(); 
     datasource = new CommentsDataSource(context); 
     gps = new GPSTracker(context); 
     datasource.open(); 
     mHelloTo = datasource.getComment_1(); 
     datasource.close(); 
    } 
} 

在上面的代码用于检索后台服务从内部数据库中的一些数据。它工作正常,但有时它会给出以下错误,即使我正确关闭游标数据库和dbhelper。

01-08 14:31:58.691: E/SQLiteDatabase(13854): close() was never explicitly called on database '/data/data/org.apache.cordova.example/databases/commments.db' 
+0

不知道这个的MySQLiteHelper相关部分,但你可以尝试移动内部getComment_1(以下简称“cursorToComment”代码)(又名破坏“cursorToComment”方法) – hovanessyan

回答

0

尝试修改此:

public String getComment_1() { 

     open(); // open database 

     List<Comment> comments = new ArrayList<Comment>(); 

     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
       allColumns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      Comment comment = cursorToComment(cursor); 
      comments.add(comment); 
      cursor.moveToNext(); 
     } 
     // make sure to close the cursor 
     cursor.close(); 

     close(); // close database 

     return comments.get(0).getComment(); 


    } 

还什么dbHelper.close();呢?你正在关闭与db调用database.close()的连接,那么为什么你需要这个?

0

你正确地调用方法,但请在open()close()方法更改为以下:

public void open() throws SQLException { 
    dbHelper= new MySQLiteHelper (ctx); 
    database = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    dbHelper.close(); 
} 

,而不是开放,但每一次你做一个读/写操作时关闭数据库,我'd推荐你open()数据库一次在你的Application.onCreate()。关闭将在您的应用程序退出或被Android OS关闭时自动完成。由于SQLite的数据完整性,您不必担心丢失数据。

1

这只发生在JellyBean之前的平台上。果冻豆和后来有这个错误信息被删除。

即使它使用错误日志级别进行记录并且包含异常堆栈跟踪,但它不是一个抛出的异常。 SQLiteDatabase构造函数只是将其调用者的堆栈跟踪存储在成员变量异常中,并且在终结器中记录了数据库仍然打开时的堆栈跟踪。

您可以看到堆栈跟踪以查看未关闭的数据库的打开位置。

为了让你的代码具体的帮助,包括在问题