我的同步例程使用Content Providers
和Sync Adapters
。
我的例程接收一个JSONObject
并插入或更新条目。
为了决定是更新还是插入,我们检查数据库中是否存在该条目。这就是sqlite错误发生的地方。
06-03 10:58:21.239: INFO/Database(340): sqlite returned: error code = 17, msg = prepared statement aborts at 45: [SELECT * FROM table WHERE (id = ?) ORDER BY id]
我做了一些研究,发现了关于这个主题的讨论。从这次讨论中,我了解到必须调用sqlite_exec()
。我将如何在内容提供商中实现这一点?
编辑
插入/更新检查
// Update or Insert
ContentValues cv = new ContentValues();
/* put info from json into cv */
if(mContentResolver.update(ClientsProvider.CONTENT_URI, cv, null, null) == 0) {
// add remote id of entry
cv.put("rid", o.optInt("id"));
mContentResolver.insert(ClientsProvider.CONTENT_URI, cv);
}
ContentProvider::更新
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
switch(uriMatcher.match(uri)) {
case CLIENTS:
count = clientDB.update(TABLE_NAME, values, selection, selectionArgs);
break;
case CLIENT_ID:
count = clientDB.update(TABLE_NAME, values, ID + " = " + uri.getPathSegments().get(0) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
default:
count = 0;
}
return count;
}
问题已解决。我不知道为什么,但在一个模拟器图像擦除后,一切都按照它应该做的方式工作。谢谢你的时间,塞尔文!