从 NSMutable 字典中分离键和对象,并在 sqlite 的插入命令中使用值

发布于 2024-11-01 18:14:12 字数 228 浏览 2 评论 0原文

大家好,我正在 iPhone 中开发一个 sqlite 应用程序。 因为我是这个应用程序的新手,所以我不知道如何在 sqlite 的插入语句命令中使用 NSMutableDictionary 中的键和对象。 例如,我想要以下格式的插入语句。

INSERT INTO TABLENAME(列名作为 NSMUTABLEDICTIONARY 中的键)值(值作为 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 (COLUMN NAMES AS KEYS FROM NSMUTABLEDICTIONRY) VALUES (VALUES AS OBJECTS FROM NSMUTABLEDICTIONARY)
please help me out.
thanks

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

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

发布评论

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

评论(3

尘曦 2024-11-08 18:14:12

我实际上对 SQL 知之甚少,但也许这样的东西会对你有所帮助:

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

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
梦巷 2024-11-08 18:14:12
NSArray *keys = [dictionary allKeys];
NSMutableArray *values = [NSMutableArray array];
for (id k in keys)
    [values addObject:[dictionary objectForKey:k]];
NSArray *keys = [dictionary allKeys];
NSMutableArray *values = [NSMutableArray array];
for (id k in keys)
    [values addObject:[dictionary objectForKey:k]];
柠檬 2024-11-08 18:14:12

您可以使用快速枚举来循环字典:

NSDictionary *abc;
for (NSString *key in abc) {
     NSObject *foo = [abc objectForKey:key];
}

这样就可以很容易地构建您的语句。

You can use fast enumeration to loop through the dictionary:

NSDictionary *abc;
for (NSString *key in abc) {
     NSObject *foo = [abc objectForKey:key];
}

With that it should be easy to construct your statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文