Sunday, 25 June 2017

mysql - How to avoid query in sqlite objective c. Take almost 2min to add data in sqlite iOS. Shall we add data as an array in sqlite?

This is a park inspection app. So ParkID,Inspection ID,Parkitem all are related to park. All are string and integer



+ for (NSDictionary *inspectionDictionary in dictionary[@"inspection"]) 
{
Inspection *inspection = [[Inspection alloc]
initWithDictionary:inspectionDictionary];


inspection.parkID = [dictionary[@"parkid"] integerValue];

[[Model sharedModel].dbHelper insertOrUpdateInspection:inspection];

}




+ (BOOL)insertOrUpdateParkItem:(ParkItem *)parkItem 

{

if ([self parkItemExists:parkItem.itemID withParkId:parkItem.parkID
andInspectionId:parkItem.inspectionID])

{
return [self updateParkItem:parkItem];
}
else
{

return [self insertParkItem:parkItem];
}
}




+ (BOOL)inspectionExists:(NSInteger)inspectionId inPark:(NSInteger)parkId 
{


int count = 0;

sqlite3 *database;

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)

{

NSString *query = [NSString stringWithFormat:@"SELECT COUNT(*) FROM inspection WHERE park_id = %ld AND inspection_id = %ld", (long)parkId, (long)inspectionId];


const char* sqlStatement = (const char*)[query UTF8String];

sqlite3_stmt *statement;


if( sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK )

{

while( sqlite3_step(statement) == SQLITE_ROW )


{

count = sqlite3_column_int(statement, 0);

} }

else

{


return NO;

}


sqlite3_finalize(statement);

sqlite3_close(database);
}

if (count > 0) {
return YES;
}
return NO;
}




+ (BOOL)insertParkItem:(ParkItem *)parkItem 

{

sqlite3 *database;

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)

{
NSString *query = [NSString stringWithFormat:@"INSERT INTO
park_item (item_id, park_id,inspection_id,
item_name) VALUES (%ld, %ld,%ld, '%@')",

(long)parkItem.itemID, (long)parkItem.parkID,
(long)parkItem.inspectionID,parkItem.itemName];

const char* sqlStatement = (const char*)[query UTF8String];

char *errmsg = nil;

if(sqlite3_exec(database, sqlStatement, NULL, NULL, &errmsg)==SQLITE_OK)

{

sqlite3_close(database);
return YES;
}
else
{
return NO;
}
}
return NO;
}





- (BOOL)updateParkItem:(ParkItem *)parkItem 
{
sqlite3 *database;

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)


{

NSString *query = [NSString stringWithFormat:@"UPDATE park_item SET
item_name= '%@' WHERE item_id = %ld AND park_id=%ld AND
inspection_id=%ld", parkItem.itemName,
(long)parkItem.itemID, (long)parkItem.parkID,
(long)parkItem.inspectionID];

const char* sqlStatement = (const char*)[query UTF8String];


char *errmsg = nil;

if(sqlite3_exec(database, sqlStatement, NULL, NULL, &errmsg)==SQLITE_OK)

{

sqlite3_close(database);
return YES;
}
else

{
return NO;
}
}
return NO;
}


In android there is facility to add data as an array. They are not using Query to add data in sqlite




So there is anything like that in iOS.. Please Help...

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...