Почему мой код только вставляет первый элемент TIMEPROFILESUBSEGS в БД? - PullRequest
2 голосов
/ 22 мая 2019

Я пытаюсь зациклить JSON и вставить данные в БД SQLite.Но у меня проблема, когда вставляется только первый элемент TIMEPROFILESUBSEGS.

После установки точки останова на «timeProfilesList» в нижней части моего кода, он, похоже, содержит все правильные данные.Я не совсем уверен, в чем проблема.

Данные:

[{"TZID": "00158ffcb97a4c4b9e065546bb33881d", "TZITEMID": 9, "TZNAME": "Port","ЦВЕТ": "# 15d8ea", "TIMEPROFILESUBSEGS": [{ "TZIID": 4371, "SERIAL": 1, "TimeSlice": "20: 08; 20: 08; 1; 1; 1; 0; 1;1; 1; 1; 1; 1 "}, {" TZIID ": 4372," SERIAL ": 2," TimeSlice ":" 20: 06; 20: 06; 0; 0; 0; 1; 0; 0;0; 0; 0; 0 "}, {" TZIID ": 4373," SERIAL ": 3," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0;0; 0; 0 "}, {" TZIID ": 4374," SERIAL ": 4," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0;0; 0 "}, {" TZIID ": 4375," SERIAL ": 5," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0;0 "}, {" TZIID ": 4376," SERIAL ": 6," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0"}, { "TZIID": 4377, "SERIAL": 7, "TimeSlice": "00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0"},{ "TZIID": 4378, "SERIAL": 8, "TimeSlice": "00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0"}, {»TZIID ": 4379," SERIAL ": 9," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0 "}, {" TZIID»: 4380, "SERIAL": 10, "TimeSlice": "00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0"}, { "TZIID": 4381, "SERIAL": 11, "TimeSlice": "00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0"}, { "TZIID": 4382,»SERIAL": 12," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0 "}]}, {" TZID ":" 0d06849e96d24ce5babecfa28c9e56be»,"TZITEMID": 10, "TZNAME": "2 вечера", "ЦВЕТ": "# 000000", "TIMEPROFILESUBSEGS": [{ "TZIID": 4407, "SERIAL": 1, "TimeSlice": "13:00;19: 10; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1 "}, {" TZIID ": 4408," SERIAL ": 2," TimeSlice ":" 00: 00; 00:00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4409," SERIAL ": 3," TimeSlice ":" 00: 00; 00: 00;0; 0; 0; 0; 0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4410," SERIAL ": 4," TimeSlice ":" 00: 00; 00: 00; 0;0; 0; 0; 0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4411," SERIAL ": 5," TimeSlice ":" 00: 00; 00: 00; 0; 0;0; 0; 0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4412," SERIAL ": 6," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0;0; 0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4413," SERIAL ": 7," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0;0; 0; 0; 0; 0; 0 "}, {" TZIID ": 4414," SERIAL ": 8," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0;0; 0; 0; 0; 0 "}, {" TZIID ": 4415," SERIAL ": 9," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0;0; 0; 0; 0 "}, {" TZIID ": 4416," SERIAL ": 10," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0;0; 0; 0 "}, {" TZIID ": 4417," SERIAL ": 11," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0;0; 0 "}, {" TZIID ": 4418," SERIAL ": 12," TimeSlice ":" 00: 00; 00: 00; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0 "}]}

Код:

NSArray *timprofiles =[jsonData objectForKey:@"TIMEPROFILE"];


// NSLog(@"DATA LOG %@", timprofiles);

for (NSDictionary * subObject in timprofiles)
{

    NSString *tziid = @"";
    NSString *serial = @"";
    NSString *to_time = @"";
    NSString *from_time = @"";
    NSString *monday = @"";
    NSString *tueday = @"";
    NSString *wednesday = @"";
    NSString *thursday = @"";
    NSString *friday = @"";
    NSString *saturday = @"";
    NSString *sunday = @"";
    NSString *holiday = @"";
    NSString *special_day_one = @"";
    NSString *special_day_two = @"";

    //  NSDictionary *subObject = [timprofiles objectAtIndex:i];

    NSString *tzid =[subObject objectForKey:@"TZID"];
    NSString *tzitemid =[subObject objectForKey:@"TZITEMID"];


    NSArray *timeprofiledegments =[subObject objectForKey:@"TIMEPROFILESUBSEGS"];

    NSLog (@"time profile segments %@", timeprofiledegments);




    for (NSDictionary * arryObjects in timeprofiledegments)
    {


        // NSDictionary *arryObjects = [timeprofiledegments objectAtIndex:x];

        tziid =[arryObjects objectForKey:@"TZIID"];
        serial =[arryObjects objectForKey:@"SERIAL"];


        NSString *timeSegList =[arryObjects objectForKey:@"TIMESLICE"];
        NSArray *splitarray =[timeSegList componentsSeparatedByString:@";"];


        for (int j = 0; j < splitarray.count; j++)
        {
            switch (j)
            {
                case 0:
                    from_time =[splitarray objectAtIndex:j];
                    break;
                case 1:
                    to_time =[splitarray objectAtIndex:j];
                    break;
                case 2:
                    monday =[splitarray objectAtIndex:j];
                    break;
                case 3:
                    tueday =[splitarray objectAtIndex:j];
                    break;
                case 4:
                    wednesday =[splitarray objectAtIndex:j];
                    break;
                case 5:
                    thursday =[splitarray objectAtIndex:j];
                    break;
                case 6:
                    friday =[splitarray objectAtIndex:j];
                    break;
                case 7:
                    saturday =[splitarray objectAtIndex:j];
                    break;
                case 8:
                    sunday =[splitarray objectAtIndex:j];
                    break;
                case 9:
                    holiday =[splitarray objectAtIndex:j];
                    break;
                case 10:
                    special_day_one =[splitarray objectAtIndex:j];
                    break;
                case 11:
                    special_day_two =[splitarray objectAtIndex:j];
                    break;
                default:
                    break;
            }


            [tpOP insertTP:timeProfilesList];

        }


        // NSLog(@"timeprofiledata %@", timeProfilesList);

        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      tziid]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      tzid]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      serial]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      from_time]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      to_time]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      monday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      tueday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      wednesday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      thursday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      friday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      saturday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      sunday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      holiday]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      special_day_one]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      special_day_two]];
        [timeProfilesList addObject: [NSString stringWithFormat:@"%@",
                                      tzitemid]];
    }            //end of second for loop



}                //end of forloop
[tpOP insertTP:timeProfilesList];
NSLog (@"done");
[authop addFullSync];
NSLog (@"TIMEPROFILE list array %@", timeProfilesList);

Вставка SQLite

sqlite3_stmt *statement;
NSString *SQLInsert = @"INSERT INTO TIME_PROFILES (TIMEZONE_IID, TIMEZONE_ID, SERIAL, FROM_TIME, TO_TIME ,MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, HOLIDAY, SPECIAL_DAY_ONE, SPECIAL_DAY_TWO, TIMEZONE_ITEM_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
if (sqlite3_prepare_v2(database, [SQLInsert UTF8String], -1, &statement, nil) == SQLITE_OK) {
    for (NSInteger i = 0; i < tpInfo.count; i += 16) {
        sqlite3_bind_text(statement, 1, [tpInfo[i] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 2, [tpInfo[i+1] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 3, [tpInfo[i+2] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 4, [tpInfo[i+3] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 5, [tpInfo[i+4] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 6, [tpInfo[i+5] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 7, [tpInfo[i+6] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 8, [tpInfo[i+7] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 9, [tpInfo[i+8] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 10, [tpInfo[i+9] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 11, [tpInfo[i+10] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 12, [tpInfo[i+11] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 13, [tpInfo[i+12] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 14, [tpInfo[i+13] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 15, [tpInfo[i+14] UTF8String], -1, NULL);
        sqlite3_bind_text(statement, 16, [tpInfo[i+15] UTF8String], -1, NULL);

        if (sqlite3_step(statement) != SQLITE_DONE) {
            // NSAssert1(0, @"Cannot Update Table", error);
        }
        sqlite3_reset(statement);
    }
    sqlite3_finalize(statement);
} else {
    NSLog(@"Can't prepare: %s", sqlite3_errmsg(database));
}
sqlite3_close(database);

1 Ответ

0 голосов
/ 22 мая 2019

Я думаю, что у твоих тимпрофиль есть 2 объекта, пожалуйста, попробуйте после звонка

  [tpOP insertTP:timeProfilesList]

внутри, снаружи для петли

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...