Sqlite插入或替换不会更新一列

时间:2013-11-29 07:59:00

标签: ios iphone sqlite

我正在使用Insert或Update进行数据处理。我有一个表sub_sub_categories,有一列is_favorite默认值为0,我将其设置为1当用户添加到收藏夹时。在每次应用启动时,我都会插入或替换来自网络的所有数据当我这样做时,它也将is_favorite设置为0.虽然我没有设置它。如何阻止它更新is_favorite列。这是我的代码。

for (int i=0; i < arraySubSubCategories.count; i++) {
    id dict = [arraySubSubCategories objectAtIndex:i];

    NSString *query = [NSString stringWithFormat:@"INSERT OR REPLACE into sub_sub_categories (id, name, sub_category_id, problem_name, problem_description, why, why_link, solution, solution_link, sort_order, is_deleted) VALUES (%@, '%@', %@, '%@', '%@', '%@', '%@', '%@', '%@', %@, %@)",
                       [dict objectForKey:@"id"],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"name"]],
                       [dict objectForKey:@"sub_category_id"],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"problem_name"]],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"problem_description"]],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"why"]],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"why_link"]],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"solution"]],
                       [MyCommonFunctions addSlashes:[dict objectForKey:@"solution_link"]],
                       [dict objectForKey:@"sort_order"],
                       [dict objectForKey:@"is_deleted"]];

    [MyCommonFunctions InsUpdateDelData:query];
}

修改查询为:

INSERT OR REPLACE into sub_sub_categories (id, name, sub_category_id, problem_name, problem_description, why, why_link, solution, solution_link, sort_order, is_deleted) VALUES (93, 'some data', 6, '', 'some data', 'some data', '', 'some data', 'some data', 0, 0)

1 个答案:

答案 0 :(得分:2)

INSERT OR REPLACE不会在db中保留以前的值,它会使用给定的值或默认值覆盖它。

您必须首先查询数据库以获取现有字段(如果存在记录),并在查询中放置您不想替换的字段。