FMDB 字典查询

发布于 2025-01-08 02:42:58 字数 1382 浏览 1 评论 0原文

我需要运行一个查询,看起来像是

INSERT INTO Appointments (field1, field2, field3, ..., field30) VALUES (value1, value2, value3, ..., value30)

我的约会存储在字典中,并且希望循环遍历该字典以使键等于字段并且值等于值。

我正在尝试使用 executeUpdate:... withParameterDictionary:... 但如果我不知道字段名称,则无法弄清楚如何使其适用于多个字段。字段名称是通过 JSON 发送的,我不想手动输入 30 个字段,而是想循环遍历字典并以这种方式获取它们。

我什至尝试过

NSMutableArray *keys = nil;
 NSMutableArray *values = nil;

        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            keys = [NSMutableArray array];
            values = [NSMutableArray array];
            for (id key in dict) {
                [keys addObject:key];
                [values addObject:[NSString stringWithFormat:@":%@", key]];
            }
            NSString *keyString = [keys componentsJoinedByString:@","];
            NSString *valueString = [values componentsJoinedByString:@","];
            [[dataObj db] executeUpdate:@"DELETE FROM Appointments"];
            NSLog(@"INSERT INTO Appointments (%@) VALUES (%@)", keyString, valueString);
            [[dataObj db] executeUpdate:@"INSERT INTO Appointments (?) VALUES (?)", keyString, valueString];

        }

上面的代码打印 NSLog 查询应该如何显示,但没有任何内容被插入到数据库中。我知道这一点是因为我在查询运行后打开模拟器数据库文件,但它仍然是空白的。

如何使上述代码正常工作,或者如何使 executeQuery:... withParameterDictionary:... 处理多个名称。

I need to run a query that looks would look like

INSERT INTO Appointments (field1, field2, field3, ..., field30) VALUES (value1, value2, value3, ..., value30)

I have my Appointments being stored inside a Dictionary and would like to loop through that dictionary to make the keys equal the fields and the values equal the values.

I'm trying to use the executeUpdate:... withParameterDictionary:... but can't figure out how to make that work with multiple fields if I don't know the field names. The field names are being sent via JSON and instead of manually typing out 30 fields I would just like to loop through the dictionary and get them that way.

I have even tried

NSMutableArray *keys = nil;
 NSMutableArray *values = nil;

        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            keys = [NSMutableArray array];
            values = [NSMutableArray array];
            for (id key in dict) {
                [keys addObject:key];
                [values addObject:[NSString stringWithFormat:@":%@", key]];
            }
            NSString *keyString = [keys componentsJoinedByString:@","];
            NSString *valueString = [values componentsJoinedByString:@","];
            [[dataObj db] executeUpdate:@"DELETE FROM Appointments"];
            NSLog(@"INSERT INTO Appointments (%@) VALUES (%@)", keyString, valueString);
            [[dataObj db] executeUpdate:@"INSERT INTO Appointments (?) VALUES (?)", keyString, valueString];

        }

The code above prints the NSLog how the query should looks but nothing is being inserted into the database. I know this because I am opening the simulator database file after the queries run and it is still blank.

How can I get the above code to work or how can I get the executeQuery:... withParameterDictionary:... to work with multiple names.

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

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

发布评论

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

评论(3

北城孤痞 2025-01-15 02:42:58

我进行了一些快速测试,这对我有用:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:@"AAAA44", @"a", @"BBBB44", @"b", @"CCCC44", @"c", nil];
NSMutableArray* cols = [[NSMutableArray alloc] init];
NSMutableArray* vals = [[NSMutableArray alloc] init];
for (id key in dict) {
    [cols addObject:key];
    [vals addObject:[dict objectForKey:key]];
}
NSMutableArray* newCols = [[NSMutableArray alloc] init];
NSMutableArray* newVals = [[NSMutableArray alloc] init];
for (int i = 0; i<[cols count]; i++) {
    [newCols addObject:[NSString stringWithFormat:@"'%@'", [cols objectAtIndex:i]]];
    [newVals addObject:[NSString stringWithFormat:@"'%@'", [vals objectAtIndex:i]]];
}
NSString* sql = [NSString stringWithFormat:@"insert into test (%@) values (%@)", [newCols componentsJoinedByString:@", "], [newVals componentsJoinedByString:@", "]];
NSLog(@"%@", sql);
BOOL updateSuccess = [db executeUpdate:sql];

技巧是将 ' 添加到数组中的数据中。

I ran a couple of quick tests, and this works for me:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:@"AAAA44", @"a", @"BBBB44", @"b", @"CCCC44", @"c", nil];
NSMutableArray* cols = [[NSMutableArray alloc] init];
NSMutableArray* vals = [[NSMutableArray alloc] init];
for (id key in dict) {
    [cols addObject:key];
    [vals addObject:[dict objectForKey:key]];
}
NSMutableArray* newCols = [[NSMutableArray alloc] init];
NSMutableArray* newVals = [[NSMutableArray alloc] init];
for (int i = 0; i<[cols count]; i++) {
    [newCols addObject:[NSString stringWithFormat:@"'%@'", [cols objectAtIndex:i]]];
    [newVals addObject:[NSString stringWithFormat:@"'%@'", [vals objectAtIndex:i]]];
}
NSString* sql = [NSString stringWithFormat:@"insert into test (%@) values (%@)", [newCols componentsJoinedByString:@", "], [newVals componentsJoinedByString:@", "]];
NSLog(@"%@", sql);
BOOL updateSuccess = [db executeUpdate:sql];

The trick is to add ' to the data in the arrays.

裸钻 2025-01-15 02:42:58
NSDictionary *argsDict 
    = [NSDictionary dictionaryWithObjectsAndKeys:@"My Name", 
       @"name", nil];

[db executeUpdate:@"INSERT INTO myTable (name) VALUES (:name)"
    withParameterDictionary:argsDict];
NSDictionary *argsDict 
    = [NSDictionary dictionaryWithObjectsAndKeys:@"My Name", 
       @"name", nil];

[db executeUpdate:@"INSERT INTO myTable (name) VALUES (:name)"
    withParameterDictionary:argsDict];
_畞蕅 2025-01-15 02:42:58

这是我刚刚编写的一些示例代码,用于在插入时支持可选值。只是简单测试过,但我认为它有效。

    NSMutableDictionary* fieldsandvalues = [NSMutableDictionary dictionary];
    fieldsandvalues[@"word"] = userphrase.word;
    fieldsandvalues[@"translation"] = userphrase.translation;
    if (userphrase.samplesentence.length > 0) {
        fieldsandvalues[@"samplesentence"] = userphrase.samplesentence;
    }
    if (userphrase.notes.length > 0) {
        fieldsandvalues[@"notes"] = userphrase.notes;
    }

    NSMutableArray* keyswithcolon = [NSMutableArray array];
    for (NSString* key in fieldsandvalues.allKeys) {
        [keyswithcolon addObject:[NSString stringWithFormat:@":%@", key]];
    }

    NSString* sql = [NSString stringWithFormat:@"INSERT INTO userphrase (%@) VALUES (%@)", [fieldsandvalues.allKeys componentsJoinedByString:@","], [keyswithcolon componentsJoinedByString:@","]];
//  DLog(@"sql: %@", sql);
    if (![self.db executeUpdate:sql withParameterDictionary:fieldsandvalues]) {
        NSAssert(NO, @"Failed inserting userphrase into database! Last error: %@ - %@", self.db.lastError, self.db.lastErrorMessage);
        return nil;
    }

Here is some sample code I just wrote to support optional values at insert time. Just briefly tested but I think it works.

    NSMutableDictionary* fieldsandvalues = [NSMutableDictionary dictionary];
    fieldsandvalues[@"word"] = userphrase.word;
    fieldsandvalues[@"translation"] = userphrase.translation;
    if (userphrase.samplesentence.length > 0) {
        fieldsandvalues[@"samplesentence"] = userphrase.samplesentence;
    }
    if (userphrase.notes.length > 0) {
        fieldsandvalues[@"notes"] = userphrase.notes;
    }

    NSMutableArray* keyswithcolon = [NSMutableArray array];
    for (NSString* key in fieldsandvalues.allKeys) {
        [keyswithcolon addObject:[NSString stringWithFormat:@":%@", key]];
    }

    NSString* sql = [NSString stringWithFormat:@"INSERT INTO userphrase (%@) VALUES (%@)", [fieldsandvalues.allKeys componentsJoinedByString:@","], [keyswithcolon componentsJoinedByString:@","]];
//  DLog(@"sql: %@", sql);
    if (![self.db executeUpdate:sql withParameterDictionary:fieldsandvalues]) {
        NSAssert(NO, @"Failed inserting userphrase into database! Last error: %@ - %@", self.db.lastError, self.db.lastErrorMessage);
        return nil;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文