SQLite“数据库模式已更改”内容提供者错误

发布于 2024-11-11 14:26:08 字数 1523 浏览 4 评论 0原文

我使用 Content ProvidersSync 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]

我做了一些研究,发现this关于该主题的讨论。从这次讨论中我了解到必须调用 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::update

@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;
}

I'm using Content Providers and Sync Adapters for my synchronization routine.

My routine receives a JSONObject and insert or update the entry.

In order to decide if we are going to update or insert we check if the entry exists in the database. This is where the sqlite error occurs.

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]

I have done some research and found this discussion about the subject. From this discussion I understand that sqlite_exec() has to be called. How would I implement this in a Content Provider?

Edit

Insert / Update check

// 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::update

@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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-11-18 14:26:08

问题解决了。我不知道为什么,但在模拟器图像擦除后,一切都完全按照其应该的方式工作。感谢您抽出宝贵时间接受塞尔文!

Problem is solved. I'm not sure why but after an emulator image wipe everything works exactly how its supposed to do. Thank you for your time Selvin!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文