【问题标题】:A SQLiteConnection object for database Please fix数据库的 SQLiteConnection 对象请修复
【发布时间】:2014-04-11 09:24:48
【问题描述】:
04-11 05:05:17.837: W/SQLiteConnectionPool(6454): A SQLiteConnection object for database Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

这是我的 logcat 返回的错误,我在herehere 中搜索了此错误的可能解决方案,说您应该关闭数据库,但我有此代码

@Override
     protected void onPause() {
      super.onPause();

      if (db.isOpen()) {
          db.close();
      }
     }

关于我所有的活动,所以我想知道为什么会出现这个错误

【问题讨论】:

  • 你确定每个光标在你完成后都关闭了吗?
  • 即使使用上面的代码,是否有可能打开游标?我是否需要每次打开数据库以进行原始查询、插入和更新时都输入 db.close()?
  • 不需要这个,你也可以在 onDestroy 中试试
  • 还有一件事 - protected void onDestroy() { super.onDestroy(); if (db.isOpen()) { db.close(); } } 也试试这个

标签: android sqlite


【解决方案1】:
public class DatabaseHelper extends SQLiteOpenHelper { 

  private static DatabaseHelper sInstance;

  private static final String DATABASE_NAME = "database_name";
  private static final String DATABASE_TABLE = "table_name";
  private static final int DATABASE_VERSION = 1;

  public static DatabaseHelper getInstance(Context context) {

    // Use the application context, which will ensure that you 
    // don't accidentally leak an Activity's context.
    // See this article for more information: http://bit.ly/6LRzfx
    if (sInstance == null) {
      sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
  }

  /**
   * Constructor should be private to prevent direct instantiation.
   * make call to static factory method "getInstance()" instead.
   */
  private DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

我目前正在做同样的事情我正在做这个教程如果你想尝试它你可以找到它here

【讨论】:

  • 你能帮忙解决这个问题吗issue
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2010-09-24
  • 2020-10-20
  • 2017-02-05
  • 2023-03-05
  • 2021-11-03
相关资源
最近更新 更多