2012-02-22 61 views
3

我需要運行一個查詢,看起來會是什麼樣子FMDB查詢與詞典

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:...與多個名稱的工作。

回答

6

我跑了幾個快速測試,這對我的作品:

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]; 

訣竅是添加'到陣列中的數據。

+0

你是對的,我昨天做了這個,並得到它的工作,但忘了發佈。我會測試你發佈的字典內容。 – Bot 2012-02-23 19:06:51

+3

這太好了。唯一的缺點是你跳過FMDB中的所有內置轉義。這可能會被注入取決於你如何獲得所有參數。 – Julian 2012-08-09 06:20:05

+0

我碰到了類似的問題,無法弄清楚如何使用的executeQuery withParameterDictionary和自動遞增的關鍵。 這裏介紹的方法將工作,但繞過內置逃脫,這是一個令人失望:( – selytch 2013-02-23 10:00:38

2
NSDictionary *argsDict 
    = [NSDictionary dictionaryWithObjectsAndKeys:@"My Name", 
     @"name", nil]; 

[db executeUpdate:@"INSERT INTO myTable (name) VALUES (:name)" 
    withParameterDictionary:argsDict]; 
0

以下是我剛剛寫入的一些示例代碼,以便在插入時支持可選值。只是簡單的測試,但我認爲它的作品。

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