【问题标题】:How to close Cursor in getContentResolver for update method on android如何在getContentResolver中关闭光标以获取android上的更新方法
【发布时间】:2015-03-20 06:42:41
【问题描述】:

您好,在我的项目中,我正在使用 getContentResolver 方法在 for 循环中更新数据库。这是我用于更新数据库的编码

for(VOShoplyRetailer retailer:retailerMasterList){
            ContentValues retailerCV = retailer.parseToContentValues();
            int affectedRows = context.getContentResolver().update(DataContract.RetailerMasterEntry.RETAILER_MASTER_CONTENT_URI,
                    retailerCV,
                    DataContract.RetailerMasterEntry.COLUMN_RETAILER_ID+" = ?",
                    new String[]{retailer.getRetailerId()}
            );
        }

我收到错误

03-20 11:55:45.279 6777-6786/com.example W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '+data+data+com_example+databases+example_db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 03-20 11:55:45.299 6777-6786/com.example W/CursorWrapperInner﹕ Cursor finalized without prior close()

如何在 getContentResolver 中关闭光标(或)如何修复此错误。

【问题讨论】:

  • 你在哪里使用 ContentResolver.query() ?
  • 阅读此SO post。可能你不止一次打开数据库
  • 没有查询我只是使用更新方法更新

标签: android database sqlite cursor android-contentresolver


【解决方案1】:

在更新的 ContentProvider 实现中,在返回之前关闭数据库 .. 可能类似于

SQLiteDatabase db = yourDBHelper.getWritableDatabase();
int rowsAffected = db.update(table, values, whereClause, whereArgs);
db.close();
return rowsAffected;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多