我正在使用 Android 同步适配器框架将本地数据库与远程数据库同步。在 onPerformSync 中,我有一个 (ContentProviderClient provider) 的实例,它使用本地数据库完成所有工作。要访问正确的数据库,我必须提供内容 Uri(在我的例子中是 ServiceContract.CHECKINS_DATA_URI - content://authority/checkins)它在查询操作中工作正常但是当我尝试更新时数据库我收到错误:
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。您认为什么会导致此错误?
公共(public)类 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();
}
}
}
最佳答案
我终于知道是怎么回事了。我是 Sync Adapter 框架的新手,所以我没有足够清楚地了解它是如何工作的。同步适配器中的更新方法实际上是我用来处理本地数据库的常规内容提供程序的更新方法。而且之前没有测试过。
关于Android + Sync Adapter + Content provider更新操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33221566/