【问题标题】:Sqlite memory leak. Bad tutorial or it's usage?Sqlite 内存泄漏。糟糕的教程或它的用法?
【发布时间】:2015-09-27 17:56:55
【问题描述】:

我正在尝试构建我的第一个应用程序,但在使用 sqlite 时遇到了困难。

我明白了

W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

作为初学者,我使用了Sqlite tutorial。据我了解,我必须在 DB 类的末尾编写公共方法并像 3 中所示那样调用它。

但 Android Studio 有时会显示内存泄漏。

我注意到任何地方都没有任何 db.close。所以我在调用方法中添加了它,后来是公共方法。还是有漏的。现在我读到那个游标也必须关闭。

我是不是用错了这个教程?还是教程不好?

【问题讨论】:

    标签: android sqlite memory-leaks sqliteopenhelper


    【解决方案1】:

    这终于是你用过的教程了。 if (cursor.moveToFirst()) {....}之后必须有一个cursor.close();

    仍然: 我认为有更好的教程可用。本教程中的某些内容没有意义,例如 while 循环周围的 cursor.moveToFirst()。 I would do this on this way.

    【讨论】:

      【解决方案2】:

      我是不是用错了这个教程?还是教程不好?

      这是一个非常糟糕的教程。除了您指出的错误之外,它还对文件系统路径进行了硬编码。 绝不使用硬编码文件系统路径。它也完全打破了SQLiteOpenHelper 模型。

      如果想在你的应用中打包数据库的具体情况,请use SQLiteAssetHelper。

      【讨论】:

        猜你喜欢
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 2011-06-11
        • 2019-11-14
        • 2012-06-16
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        相关资源
        最近更新 更多