close() was never explicitly called on database 和 database is locked 错误原因
来源:互联网 发布:线切割3b编程图片 编辑:程序博客网 时间:2024/05/29 16:10
1.cursor 使用完没有关闭。
2.数据库用完没有关闭。
3.数据库重复新建(new了多个对象)。
我的错误是new了多个对象。
LogCat 报错信息:
- 02-13 11:58:13.759: ERROR/Database(432): close() was never explicitly called on database '/data/data/com.yyt.ui/databases/yyt.db'
- 02-13 11:58:13.759: ERROR/Database(432): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
- 02-13 11:58:13.759: ERROR/Database(432): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1847)
- 02-13 11:58:13.759: ERROR/Database(432): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
- 02-13 11:58:13.759: ERROR/Database(432): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
- 02-13 11:58:13.759: ERROR/Database(432): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
- 02-13 11:58:13.759: ERROR/Database(432): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
- 02-13 11:58:13.759: ERROR/Database(432): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
- 02-13 11:58:13.759: ERROR/Database(432): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
- 02-13 11:58:13.759: ERROR/Database(432): at com.taobao.htc.db.RecordDBHelper.query(RecordDBHelper.java:49)
- 02-13 11:58:13.759: ERROR/Database(432): at com.taobao.htc.manage.RecordDBopt.get(RecordDBopt.java:78)
- 02-13 11:58:13.759: ERROR/Database(432): at com.taobao.htc.manage.RecordMessage.sendMseeage(RecordMessage.java:111)
- 02-13 11:58:13.759: ERROR/Database(432): at com.taobao.htc.manage.RecordMessage.sendMseeage(RecordMessage.java:87)
- 02-13 11:58:13.759: ERROR/Database(432): at com.taobao.htc.Report.run(Report.java:49)
- 02-13 11:58:13.759: ERROR/Database(432): at java.lang.Thread.run(Thread.java:1019)
- 02-13 11:58:13.830: WARN/ActivityManager(62): Launch timeout has expired, giving up wake lock!
- 02-13 11:58:14.381: DEBUG/dalvikvm(432): GC_CONCURRENT freed 771K, 57% free 2908K/6727K, external 2013K/2137K, paused 7ms+5ms
- 02-13 11:58:14.630: DEBUG/dalvikvm(432): GC_FOR_MALLOC freed 867K, 58% free 2836K/6727K, external 2013K/2137K, paused 178ms
发生错误原因分析:
主要错误:
- 02-13 11:58:13.759: ERROR/Database(432): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
出错处代码
- SQLiteDatabase db = getWritableDatabase();
意思是指在此处打开数据库操作之前,应用程序没有关闭游标或数据库对象。
在应用中,对数据库操作完成后,应该对游标及数据库进行关闭操作
- if (!cur.isClosed()) {
- cur.close();
- }
- db.close();
解决办法:
检查操作数据库的相关源代码,增加关闭“cursor or database object”的代码 。
总结一下Activity.startManagingCursor方法:
c=cr.query(Uri.parse(Values.contentA + Values.contentB), null, Values.table_02_name+"="+"\""+str+"\";",
null, null);
startManagingCursor(c);
c.moveToFirst();
如果操作cursor不是在Activity中,不能用startManagingCursor函数的
——————————————————————————————————我是分割线————————————————————————————————————
而当我们遇到Caused by: android.database.sqlite.SQLiteException: database is locked这个问题时,主要原因大部分都是多线程多数据库进行操作了。这个时候你需要在需要操作数据数据的时候,提供一个统一的入口,并加上同步锁,
使用synchronized 关键字来修饰获取数据库连接的方法,或者使用isDbLockedByOtherThreads方法判断数据库是否被锁住了,然后等待一定的时间再进行访问。public static synchronized DBConnection getConnection(String connectionName) throws Exception { String pathFile = getPath() + connectionName;// 转换目录data下 return new DBConnection(SQLiteDatabase.openDatabase(pathFile, null, SQLiteDatabase.OPEN_READWRITE)); }
或者使用isDbLockedByOtherThreads方法判断数据库是否被锁住了,然后等待一定的时间,并且对于数据库的操作要及时释放,以免多个线程形成死锁
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on database 和 database is locked 错误原因
- close() was never explicitly called on databaseandroid.database.sqlite异常
- FAQ_21 sqlite exception: close() was never explicitly called on database
- 处理AndroidSQLite-close()was never explicitly called on database异常
- Android SQLite - close() was never explicitly called on database
- close() was never explicitly called on database,SQLiteMisuseException
- android SQLite :close() was never explicitly called on database 'XXXXXX'
- SQLite: close() was never explicitly called on database
- 解决android sqlite 中"close() was never explicitly called on database" 与java线程同步
- 处理Android SQLite - close() was never explicitly called on database异常
- close() was never explicitly called on database '/data/data/com.example.shiyan/databases/njupt.db'
- close() was never explicitly called on database '/data/data/com.seehope.view/databases/note.db' 异常
- database is locked和SQLITE_BUSY
- sqlite出现database is locked错误
- sqlite报database is locked错误是什么原因?
- sqlite database is locked
- 如何卸载APEX
- 多线程之NSInvocationOperation
- 基于XMPP协议的Android即时通信系
- cocosBuilder
- 精选关键内容打印有绝招【xiame】
- close() was never explicitly called on database 和 database is locked 错误原因
- 思科交换机配置命令
- 如何查看你购买的虚拟主机的带宽
- zookeeper
- KVM虚拟化技术浅析
- 判断SOCKET是否还连接
- MyEclipse10.0优化
- 杂记(一)
- 三十分钟掌握STL