lijingf117
2012-10-10 17:03
浏览 375

Android数据库错误,db或者游标没有关闭,如果没有关闭,如何强制关闭呢

一般都没有问题,有时候会出现这个错误,猜想是不是程序异常结束的时候,db或者游标没有被关闭。
当再启动app的时候,通过下面这句取得数据库的时候,出现了下面的错误信息:
DatabaseOpenHelper.getWritableDatabase();

求解决方案:
加入上面的取得方法出错,捕捉错误然后先把没有关闭的先关闭了,再打开数据库。-》》如何解决呢??

10-09 19:24:41.269: E/Database(1269): close() was never explicitly called on database '/data/data/com.android.providers.media/databases/external-47cd50a6.db'
10-09 19:24:41.269: E/Database(1269): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1847)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
10-09 19:24:41.269: E/Database(1269): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:576)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
10-09 19:24:41.269: E/Database(1269): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaProvider.query(MediaProvider.java:1451)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaProvider.attachVolume(MediaProvider.java:3880)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2317)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1909)
10-09 19:24:41.269: E/Database(1269): at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
10-09 19:24:41.269: E/Database(1269): at android.content.ContentResolver.insert(ContentResolver.java:604)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaScannerService.openDatabase(MediaScannerService.java:65)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaScannerService.scanFile(MediaScannerService.java:217)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaScannerService.access$100(MediaScannerService.java:53)
10-09 19:24:41.269: E/Database(1269): at com.android.providers.media.MediaScannerService$ServiceHandler.handleMessage(MediaScannerService.java:282)
10-09 19:24:41.269: E/Database(1269): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 19:24:41.269: E/Database(1269): at android.os.Looper.loop(Looper.java:123)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • iteye_15176 2012-10-11 15:40
    已采纳

    对于Android中的SQLite数据库,我一般采用的方法是,随用随取随关。
    进行DAO操作时,首先获取一个SQLiteDatabase对象,具体代码采用try{}finally{}的格式。确保有任何问题,数据库对象都被关闭。

    打赏 评论
  • gjhappyyy 2012-10-11 13:44

    数据库用完必须关闭,使用的时候在打开。

    打赏 评论

相关推荐 更多相似问题