代码之家  ›  专栏  ›  技术社区  ›  incutonez

Android:更新另一个应用中使用的SQLite表

  •  1
  • incutonez  · 技术社区  · 6 年前

    getContentResolver().update 在每一行。理想情况下,我希望能够使用SQLite事务。 applyBatch bulkInsert 这是行不通的,因为我需要在每一行更新时都有一个WHERE子句——实际上,我必须更新一个索引表,它有多个播放列表和它们的歌曲。

    this thread

    任何关于我应该去哪里的提示,或者如果已经有了答案,那就太好了。

    1 回复  |  直到 6 年前
        1
  •  0
  •   incutonez    6 年前

    我最后做的是 ContentProviderOperation getContentResolver().applyBatch applyBatch 你必须把它包装成一个try..catch,它报告的Android Studio错误并不明显。以下是最终代码:

    Cursor songs = createPlaylistCursor(sort);
    if (songs.moveToFirst()) {
        int i = 0;
        Uri playListUri = getPlaylistUri();
        ArrayList<ContentProviderOperation> cs = new ArrayList<ContentProviderOperation>();
        do {
            cs.add(ContentProviderOperation.newUpdate(playListUri)
              .withSelection("folder_file_id=" + songs.getInt(songs.getColumnIndexOrThrow("folder_files._id")), null)
              .withValue("sort", i++)
              .build());
        } while (songs.moveToNext());
        try {
            getContentResolver().applyBatch("com.maxmpz.audioplayer.data", cs);
        }
        catch (Exception e) {
        }
    }