iOS - 将NSDictionary复制到SQLite3

时间:2014-01-06 10:52:33

标签: ios objective-c cocoa-touch

我有一个接收表名和NSDictionary的方法。在该方法中,我想要一个循环为每条记录创建一个SQL Insert语句,该语句将NSDictionary的内容复制到相应的table中。 NSDictionary已在之前的方法中通过JSON web service填充。

我遇到的问题是以正确的格式从NSDictionary和Insert语句中获取信息。

我想要的是每个记录都有一个与此类似的Insert语句:

INSERT INTO speaker VALUES (0f32ae3f-ad56-e311-a863-000c29beef51,<null>,1385478793, Mike Smith,<null>,BVSc PhD DSAM DipECVIM-ca MRCVS)

但我得到的是:

INSERT INTO speaker VALUES (
   {
        ID = "0f32ae3f-ad56-e311-a863-000c29beef51";
        ImageURL = "<null>";
        LastMod = 1385478793;
        Name = "Mike Smith";
        Profile = "<null>";
        Qualifications = "BVSc PhD DSAM DipECVIM-ca MRCVS";
   }
)  

到目前为止,这是我的代码:

-(void)populateTable:(NSString *)tableName dictonary:(NSMutableDictionary *)tempDict
{

    sqlite3_stmt *stmt;

    NSMutableString *keys = [NSMutableString string];
    NSMutableString *values = [NSMutableString string];

    for (NSString *key in tempDict.allKeys) { //edited from tempDict to tempDict.allKeys
        [keys appendFormat:@"%@,", key];
        [values appendFormat:@"%@,", [tempDict objectForKey:key]];
    }

    [values deleteCharactersInRange:NSMakeRange([values length] -1, 1)];
    NSString *queryStr = [NSString stringWithFormat:@"INSERT INTO %@ VALUES %@", tableName, values];
    NSLog(@"Query = %@", queryStr);
    NSLog(@"GetData   -   populateTable   -   qry : %@",queryStr);
    const char *sql = [queryStr UTF8String]; ;

    if((sqlite3_open([[self filePath] UTF8String], &congressDB)==SQLITE_OK))
    {
        NSLog(@"GetData   -   populateTable   -   str = %@", queryStr);
        if (sqlite3_prepare(congressDB, sql, -1, &stmt, NULL)==SQLITE_OK)
        {
            NSLog(@"GetData   -   populatTable   -   About to carry out SQL statement");
            sqlite3_step(stmt);
            sqlite3_finalize(stmt);
        }
        else
        {
            NSLog(@"GetData   -   populateTable   -   Problem with prepare statement: %s", sqlite3_errmsg(congressDB));
            NSLog(@"GetData   -   populateTable   -   stmt = %@", stmt);
        }
        sqlite3_close(congressDB);

    }

}

1 个答案:

答案 0 :(得分:4)

尝试此更正:

- (void)populateTable:(NSString *)tableName dictonary:(NSMutableDictionary *)tempDict
{

    sqlite3_stmt *stmt;

    NSMutableString *values = [NSMutableString string];

    for (NSString *key in tempDict.allKeys)
    {
        [values setString:@""];
        NSArray *array = [tempDict objectForKey:key];
        if (array.count)
        {
            NSDictionary * dict = [array objectAtIndex:0];
            for (NSString *keyInDict in dict.allKeys)
            {
                [values appendFormat:@"%@,", [dict objectForKey:keyInDict]];
            }

            [values deleteCharactersInRange:NSMakeRange([values length] -1, 1)];
            NSString *queryStr = [NSString stringWithFormat:@"INSERT INTO %@ VALUES %@", tableName, values];
            NSLog(@"Query = %@", queryStr);
            NSLog(@"GetData   -   populateTable   -   qry : %@",queryStr);
            const char *sql = [queryStr UTF8String]; ;

            if((sqlite3_open([[self filePath] UTF8String], &congressDB)==SQLITE_OK))
            {
                NSLog(@"GetData   -   populateTable   -   str = %@", queryStr);
                if (sqlite3_prepare(congressDB, sql, -1, &stmt, NULL)==SQLITE_OK)
                {
                    NSLog(@"GetData   -   populatTable   -   About to carry out SQL statement");
                    sqlite3_step(stmt);
                    sqlite3_finalize(stmt);
                }
                else
                {
                    NSLog(@"GetData   -   populateTable   -   Problem with prepare statement: %s", sqlite3_errmsg(congressDB));
                    NSLog(@"GetData   -   populateTable   -   stmt = %@", stmt);
                }
                sqlite3_close(congressDB);
            }
        }
    }
}