我正在为此应用程序编写内容提供程序,在我的内容提供程序中,我打开数据库连接,运行查询并将结果的光标返回给调用程序.如果我在提供程序中关闭此数据库连接,则游标没有结果.如果我打开它,我的DDMS日志中会出现"泄漏发现"错误.我在这里错过了什么?什么是返回数据库结果光标的干净,正确的方法?
在我的Android应用程序中,我使用SQLiteOpenHelper来实现ContentProvider.查询,添加,删除操作都是通过ContentProvider完成的.
但是在我的一部Android手机(htc g13)中,我在目录/ data/data/[包名] /数据库中找到了*.db-wal文件.使用ContentProvider进行操作时,文件大小会非常快.它占用用户RAM空间太多.
建议关闭SQLiteOpenHelper来解决我的问题(这很有用)在这里输入链接描述.
但我希望找到一个"地方"来添加"close()"方法,因为我没有直接使用SQLiteOpenHelper(通过ContentProvider使用).ContentProvider中的query()方法必须返回一个Cursor,而SQLiteDatabse应保持打开状态.
我很困惑,我现在做什么让*.db-wal消失并正常使用ContentProvider?
我有android数据库和游标的奇怪问题.不时(非常罕见)发生,我收到客户的崩溃报告.很难找到崩溃的原因,因为我有大约150,000个活跃用户,每周大约有1个报告,所以这真的是一个小错误.这是例外:
STACK_TRACE=java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
at sk.mildev84.agendareminder.a.c.a(SourceFile:169)
Run Code Online (Sandbox Code Playgroud)
在每个游标"迭代和探索"之前,我使用此代码确保一切正常:
db = instance.getWritableDatabase();
cursor = db.rawQuery(selectQuery, null);
if (isCursorEmptyOrNotPrepared(cursor)) {
...
}
private synchronized boolean isCursorEmptyOrNotPrepared(Cursor cursor) {
if (cursor == null)
return true;
if (cursor.isClosed())
return true;
if (cursor.getCount() == 0) // HERE IT CRASHES
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
它符合一致:
if (cursor.getCount() == 0)
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?我想,我正在检查所有可能的例外情况和条件......为什么我的应用程序崩溃了?
PS:所有数据库方法都是同步的,我在所有情况下都正确地打开和关闭数据库/游标,我多次检查它.
我正在使用sqlite db并使用Alex LockWood的一些代码正确管理您的SQLite数据库
它工作得很好但有时我得到错误"java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed."
这是完整的错误:
02-20 16:37:21.385: W/dalvikvm(25730): threadid=13: thread exiting with uncaught exception (group=0x41c122a0)
02-20 16:37:21.390: E/AndroidRuntime(25730): FATAL EXCEPTION: Timer-0
02-20 16:37:21.390: E/AndroidRuntime(25730): java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:963)
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:678)
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:349)
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
02-20 16:37:21.390: E/AndroidRuntime(25730): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
02-20 …Run Code Online (Sandbox Code Playgroud) 我使用ContentProvider但有时我得到标题中写的消息.是什么原因?我读到如果在关闭游标之前关闭数据库,则会出现此消息.我还读到如果我使用的话我不应该关闭光标ContentProvider