gpt4 book ai didi

ios - 为什么我的代码只将 TIMEPROFILESUBSEGS 的第一个元素插入到数据库中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:54 28 4
gpt4 key购买 nike

我正在尝试遍历 JSON 并将数据插入 SQLite 数据库。但我遇到的问题是只插入了“TIMEPROFILESUBSEGS”的第一个元素。

在我的代码底部的“timeProfilesList”上设置断点后,它似乎包含了所有正确的数据。我不完全确定问题出在哪里。

数据:

[{"TZID":"00158ffcb97a4c4b9e065546bb33881d","TZITEMID":9,"TZNAME":"端口","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"},{"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,"序列号":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"},{"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,"序列号":4,"TIMESLICE":"00:00;00:00 ;0;0;0;0;0;0;0;0;0;0"},{"TZIID":4411,"序列号":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);

最佳答案

我认为您的 timprofiles 有 2 个对象,请在调用后尝试

  [tpOP insertTP:timeProfilesList]

内外循环

关于ios - 为什么我的代码只将 TIMEPROFILESUBSEGS 的第一个元素插入到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56252702/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com