从 NSMutable 字典中分离键和对象,并使用 sqlite 的插入命令中的值 [英] separating keys and objects from NSMutable dictionary and use the values in insert command of sqlite
问题描述
大家好,我正在 iphone 中开发一个 sqlite 应用程序.因为我是这个应用程序的新手,所以我不知道如何在 sqlite 的插入语句命令中使用 NSMutableDictionary 中的键和对象.例如,我想要以下格式的插入语句.
hi folks i am developing an sqlite application in iphone. since i am new to this application, i dont how to use the key and objects from NSMutableDictionary in the command of insert statement of sqlite. for example , i want the insert statement in the following format.
INSERT INTO TABLENAME(列名作为 NSMUTABLEDICTIONRY 中的键)值(值作为 NSMUTABLEDICTIONARY 中的对象)请帮帮我.谢谢
INSERT INTO TABLENAME (COLUMN NAMES AS KEYS FROM NSMUTABLEDICTIONRY) VALUES (VALUES AS OBJECTS FROM NSMUTABLEDICTIONARY) please help me out. thanks
推荐答案
其实我对SQL知之甚少,但也许这样的东西会对你有所帮助:
I have actually very little knowledge about SQL, but maybe something like this will help you:
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"Foo", @"Bar",
@"Foz", @"Baz",
nil];
NSMutableString *keys = [NSMutableString string];
NSMutableString *values = [NSMutableString string];
for (NSString *key in myDictionary) {
[keys appendFormat:@"%@,", key];
[values appendFormat:@"%@,", [myDictionary objectForKey:key]];
}
// remove last ,
[keys deleteCharactersInRange:NSMakeRange([keys length]-1, 1)];
[values deleteCharactersInRange:NSMakeRange([values length]-1, 1)];
NSString *sqlQuery = [NSString stringWithFormat:@"INSERT INTO TABLENAME %@ VALUES %@", keys, values];
// sqlQuery = INSERT INTO TABLENAME Bar,Baz VALUES Foo,Foz
这篇关于从 NSMutable 字典中分离键和对象,并使用 sqlite 的插入命令中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!