我在我的数据库适配器中使用以下例程.每次调用它时,都会使用"应用程序未关闭光标"创建错误.
我不知道如何关闭光标,除了我是怎样的,因为我没有在调用例程中打开它.这在列表中的显示适配器中使用.
我叫它:
int cnt = mDbHelper.dbio_rcount("从mytable中选择count(*)where field1 ='V'));
public int dbio_rcount( String p_query )
{
int v_ret = 0 ;
Cursor mCursor = null ;
try
{
mCursor = mDb.rawQuery( p_query, null );
}catch (SQLException e) {}
if ( ( mCursor != null )
&& ( mCursor.moveToFirst()) )
{
v_ret = mCursor.getInt( 0 );
}
mCursor.close();
return v_ret ;
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是你通过mCursor.moveToFirst()调用得到一个异常,这意味着你的应用程序在mCursor.close()发生之前崩溃了.通常我做的是在调用moveToFirst之前检查mCursor.getCount()> 0 ()..但那只是我..我会建议:
public int dbio_rcount(String p_query)
{
int v_ret = 0 ;
Cursor mCursor = null ;
try
{
mCursor = mDb.rawQuery(p_query, null);
if (mCursor != null && mCursor.getCount() > 0)
{
mCursor.moveToFirst();
v_ret = mCursor.getInt( 0 );
}
} catch (SQLException e) {
Log.e(TAG, "sql exception in dbio_count", e);
} catch(Exception ex) {
Log.e(TAG, "other exception in dbio_count", ex);
} finally {
if (mCursor != null) {
mCursor.close();
}
}
return v_ret ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |