【问题标题】:Android + Sync Adapter + Content provider update operation failureAndroid + Sync Adapter + Content Provider 更新操作失败
【发布时间】:2015-10-19 18:39:49
【问题描述】:

我正在使用 Android Sync Adapter 框架将本地数据库与远程数据库同步。在 onPerformSync 中,我有一个 (ContentProviderClient provider) 实例,它使用本地数据库完成所有工作。要访问正确的数据库,我必须提供内容 Uri(在我的情况下是 ServiceContract.CHECKINS_DATA_URI - content://authority/checkins)它在查询操作中工作正常但是当我尝试更新时DB 我得到错误:

    android.database.sqlite.SQLiteException: near "SET": syntax error (code 1): , while compiling: UPDATE  SET status=?,timestamp=?,user_id=?,checking_id=? WHERE timestamp = ?

在 UPDATE 和 SET 运算符之间缺少表名。这有点神秘,因为我使用了正确的 Uri。您认为什么会导致此错误?

公共类 SyncAdapter 扩展 AbstractThreadedSyncAdapter {

ContentResolver mContentResolver;
SvcController mController;

public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);

    mContentResolver = context.getContentResolver();
    mController = new SvcController(context.getApplicationContext());
}

public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
    super(context, autoInitialize, allowParallelSyncs);

    mContentResolver = context.getContentResolver();
}

@Override
public void onPerformSync(Account account,
                          Bundle extras,
                          String authority,
                          ContentProviderClient provider,
                          SyncResult syncResult) {

    ContentValues cv;
    String[] projection;
    String selection;
    String[] selectionArgs;
    Uri uri;

...

    // updates checkin_id in local db
    uri = ServiceContract.CHECKINS_DATA_URI;
    selection = "timestamp = ?";

    for (CheckIn ci : checkins){

        cv = ci.toContentValues();
        selectionArgs = new String[]{String.valueOf(ci.getTimestamp().getTime())};

        try {
            provider.update(uri, cv, selection, selectionArgs);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

}

【问题讨论】:

    标签: android android-contentprovider android-syncadapter


    【解决方案1】:

    我终于知道发生了什么。我是 Sync Adapter 框架的新手,所以我没有清楚地意识到它是如何工作的。同步适配器中的 UPDATE 方法实际上是我用来处理本地数据库的常规 Content Provider 的 UPDATE 方法。而且之前没有测试过。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多