从NSMutable字典中分离键和对象,并使用sqlite的insert命令中的值 [英] separating keys and objects from NSMutable dictionary and use the values in insert command of sqlite
问题描述
大家好,我正在iphone中开发一个sqlite应用程序。
因为我是这个应用程序的新手,我不知道如何在sqlite的insert语句命令中使用NSMutableDictionary中的键和对象。例如
,我想要以下列格式的insert语句。
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(COLUMN NAMES as NSYS FROM NSMUTABLEDICTIONRY)VALUES(VALUES as Object from 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的insert命令中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!