我试图遍历JSON并将数据插入SQLite DB。但是我遇到的问题是仅插入了“ TIMEPROFILESUBSEGS”的第一个元素。
在代码底部的'timeProfilesList'上设置断点后,它似乎包含所有正确的数据。我不确定是什么问题。
数据:
[{“ TZID”:“ 00158ffcb97a4c4b9e065546bb33881d”,“ TZITEMID”:9,“ TZNAME”:“ Port”,“ COLOR”:“#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“ 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“:” 0d0684 9e96d24ce5babecfa28c9e56be“,” TZITEMID“:10,” TZNAME“:” 2pm“,” COLOR“:”#000000“,” TIMEPROFILESUBSEGS“:[{” TZIID“:4407,” SERIAL“:1,” TIMESLICE“:” 13 :00; 19:10; 1; 1; 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; 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);
答案 0 :(得分:0)
我认为您的timprofiles有2个对象,请在致电后尝试
[tpOP insertTP:timeProfilesList]
内部for循环