无法正确关闭Db

时间:2014-01-08 09:10:49

标签: android sqlite

以下是我的代码,

    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'

3 个答案:

答案 0 :(得分:1)

这只发生在JellyBean之前的平台上。 JellyBean以后删除了此错误消息。

即使以错误日志级别记录并包含异常堆栈跟踪,它也不是抛出的异常。 SQLiteDatabase构造函数只将其调用者的堆栈跟踪存储在成员变量异常中,并在终结器中记录堆栈跟踪,以防数据库仍处于打开状态。

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

要获得有关代码的具体帮助,请在问题中包含MySQLiteHelper的相关部分

答案 1 :(得分: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();的作用是什么?您正在关闭与调用database.close()的数据库的连接,那么为什么需要这个呢?

答案 2 :(得分:0)

您正确调用了这些方法,但请将open()close()方法更改为以下内容:

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

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

但是,每次进行读/写操作时,我都建议您open()数据库Application.onCreate(),而不是在每次进行读/写操作时打开和关闭数据库。关闭将在您的应用程序退出或由Android OS关闭时自动完成。由于SQLite数据完整性,您不必担心丢失数据。