0

Adding data using inspection Dictionary

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];
               
      }

Checking insert or update

+ (BOOL)insertOrUpdateParkItem:(ParkItem *)parkItem 
{
    
if ([self parkItemExists:parkItem.itemID withParkId:parkItem.parkID 
         andInspectionId:parkItem.inspectionID]) 

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

Adding data using inspection Dictionary

+ (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;
   }

Inserting to sqlite database

+ (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;
}

Updating to sqlite database

- (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...

Community
  • 1
  • 1

0 Answers0