使用sqlite更新数据库

时间:2011-12-26 06:07:51

标签: objective-c ios4

我正在进行更新查询,我正在使用Bill Weinman库进行sqlite数据库交互,所以我想更新数据库,但是本机代码仅按方案ID更新数据,但我想更新对三个值的尊重而且我是无法弄清楚我该怎么做,请帮助我。 Tnx很多。

这是控制器对象调用的方法,然后检查我管理插入和更新选项的值

// Method to insert Scenario Product detail
- (void) insertScenarioProductAttribute: (NSMutableDictionary*) getRecord andOldProductAttribute:(NSDictionary*) oldRecord {
    // NSLog(@"%s", __FUNCTION__);

    self.tableName = ktnt_scenarioproductTableName;
    NSNumber* _checkScenarioid = [self valueFromQuery:@"select scenarioid from tnt_scenarioproduct WHERE scenarioid = ? AND weekvalue = ? AND productid = ?",
                                  [oldRecord objectForKey:@"scenarioid"], [oldRecord objectForKey:@"weekvalue"], [oldRecord objectForKey:@"productid"]];
    if (_checkScenarioid) {
        [self updateRow:getRecord :_checkScenarioid];
    } else {
        [self insertRow:getRecord];     // indicate that it's a new row
    }
}

然后使用这种方法我更新数据,但这只是使用单个值"方案ID"但在这种情况下,我的所有数据都会得到我不想要的更新

- (void) updateRow:(NSDictionary *) record:(NSNumber *) rowID {
     // NSLog(@"%s", __FUNCTION__);
    int dictSize = [record count];

    // the values array is used as the argument list for bindSQL
    id keys[dictSize];  // not used, just a side-effect of getObjects:andKeys
    id values[dictSize + 1];
    [record getObjects:values andKeys:keys];    // convenient for the C array
    values[dictSize] = rowID;

    NSString * query = [NSString stringWithFormat:@"update %@ set %@ = ? where scenarioid = ?",
                        tableName,
                        [[record allKeys] componentsJoinedByString:@" = ?, "]];

    [self bindSQL:[query UTF8String] arguments:(va_list)values];
    sqlite3_step(statement);
    sqlite3_finalize(statement);
}

0 个答案:

没有答案