我有一个 Parsed Json 字符串,我想在其中循环值并将它们插入到我的 FMDB Sqlite 数据库中。我一次只能在数据库中放一条记录。我想把我所有的记录都放在 sqlite 数据库中。
我知道我必须为此编造某种循环。
我想获取下面具有多个值的已解析 json,并将其插入我的名为 unitofMeasure 和 Name 的列中。
所以我想创建一个循环来将多个 id 和多个名称插入到我的数据库中的 unitOfMeasureID 列和 Name 列中。
目前我一次只能插入我记录。
UnitOfMeasureID To Use: 1
Name: kph
UnitOfMeasureID To Use: 2
Name: kpm
UnitOfMeasureID To Use: 3
Name: kpm
for (defineJsonDataLookUp in self.jsonLookup)
{
NSNumber* UnitOfMeasureID = [defineJsonDataLookUp objectForKey:@"unitOfMeasureID"];
NSLog(@"UnitOfMeasureID To Use: %@", UnitOfMeasureID);
NSNumber* Factor = [defineJsonDataLookUp objectForKey:@"Factor"];
NSLog(@"Factor To Use: %@", Factor);
NSArray* Name = [defineJsonDataLookUp objectForKey:@"Name"]; //for actual json response
NSLog(@"Name: %@", Name); //3
NSNumber* ParentID = [defineJsonDataLookUp objectForKey:@"ParentID"];
NSLog(@"ParentID: %@", ParentID);
}
if ([strResult isEqualToString:@"[]"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message:@" Please connect to the Internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
else
{
for (defineJsonDataLookUp in self.jsonLookup)
{
NSNumber* UnitOfMeasureID = [defineJsonDataLookUp objectForKey:@"unitOfMeasureID"];
NSLog(@"UnitOfMeasureID To Use: %@", UnitOfMeasureID);
NSNumber* Factor = [defineJsonDataLookUp objectForKey:@"Factor"];
NSLog(@"Factor To Use: %@", Factor);
NSArray* Name = [defineJsonDataLookUp objectForKey:@"Name"]; //for actual json response
NSLog(@"Name: %@", Name); //3
NSNumber* ParentID = [defineJsonDataLookUp objectForKey:@"ParentID"];
NSLog(@"ParentID: %@", ParentID);
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
[db executeUpdate:[NSString stringWithFormat:@"delete from unitofmeasure"]];
[db executeUpdate:@"INSERT INTO unitofmeasure (unitOfMeasureID,Factor,Name,ParentID) VALUES (?,?,?,?)",UnitOfMeasureID,Factor,Name,ParentID];
// [db executeUpdate:@"INSERT INTO unitofmeasure (unitOfMeasureID,Name) VALUES (?,?);",
// [self.defineJsonDataLookUp objectForKey:@"unitOfMeasureID"],[self.defineJsonDataLookUp objectForKey:@"Name"], nil];
FMResultSet *results = [db executeQuery:@"select * from unitofmeasure"];
while([results next]) {
NSString *Name = [results stringForColumn:@"Name"];
NSInteger ParentID = [results intForColumn:@"ParentID"];
NSLog(@"UdadfdadfddfFuckingser: %@ - %d",Name, ParentID);
}
[db close];
}
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:0];
}
}
我的 SQlite 数据库看起来像这样,我想插入字典中的所有记录 UnitOfMeasureID: 1, Name:kph, UnitOfMeasureID: 2, Name: kpm, UnitOfMeasureID: 3, Name: kpm
问候