I have a Parsed Json string in which I would like to loop the values and insert them into my FMDB Sqlite database. I can only put one record at a time in the database. i want to put all my records in the sqlite database.
I know I have to make up some sort of loop for this.
I want to take the parsed json which has multiple values below and insert it into my column named unitofMeasure and Name.
So I want to create a loop to insert multiple id's and multiple names into the unitOfMeasureID column and Name column in my database.
Currently I can only insert i record at a time.
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];
}
}
My SQlite Database looks like this and I want to insert all the records from my dictionary UnitOfMeasureID: 1, Name:kph, UnitOfMeasureID: 2, Name: kpm, UnitOfMeasureID: 3, Name: kpm
Regards
You don't give much to go on. What's the structure of the database? What are the primary keys? Which are unique keys? What does the JSON data look like? Is it valid? Are you trying to insert what you think you are?
But, as general advice, you're ignoring error return codes.
[db executeUpdate:@"INSERT INTO unitofmeasure"];
Should be:
if(! [db executeUpdate:@"INSERT INTO unitofmeasure"]) {
// handle error
}
From the documentation:
Executing updates returns a single value, a BOOL. A return value of YES means the update was successfully executed, and a return value of NO means that some error was encountered. You may invoke the -lastErrorMessage and -lastErrorCode methods to retrieve more information
Finally, I assume this is a copy-and-paste error:
[db executeUpdate:[NSString stringWithFormat:@"delete from unitofmeasure"]];
[db executeUpdate:@"INSERT INTO unitofmeasure
The first line deletes all rows in the table. The second inserts a single row.