2016-06-17 85 views

回答

0

1步:檢查的新領域在數據庫中 第2步:使用ALTER查詢您的數據庫中添加列

嘗試下面的代碼,並應用適合自己的邏輯。在這之後反饋我。 高興地幫助你:)

+(void)alterCountryexitcodeTable{ 

    BOOL isAlterRequired = [self checkColumnExists]; 

    if (!isAlterRequired) { 
     sqlite3 *database; 
     sqlite3_stmt *statement; 
     if(sqlite3_open([kDBPath UTF8String], &database) == SQLITE_OK) 
     { 
      NSString *updateSQL = [NSString stringWithFormat: @"ALTER TABLE countries ADD COLUMN countryexitcode TEXT"]; 
      const char *update_stmt = [updateSQL UTF8String]; 
      sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL); 

      if(sqlite3_step(statement)==SQLITE_DONE) 
      { 
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB altered" message:@"Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//    [alert show]; 
//    alert=nil; 
      } 
      else 
      { 
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Updation" message:@"DB not Altered" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//    [alert show]; 
//    alert=nil; 
      } 
      // Release the compiled statement from memory 
      sqlite3_finalize(statement);  
      sqlite3_close(database); 
     } 
    } 
} 

+(BOOL)checkColumnExists 
{ 
    BOOL columnExists = NO; 

    static sqlite3 *database = nil; 
    sqlite3_stmt *selectStmt = NULL; 

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

     DLog(@"BEGIN"); 

     sqlite3_exec(database, "BEGIN", 0, 0, 0); 

     NSString *sqlStatement = @"select countryexitcode from countries"; 
     if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &selectStmt, NULL) == SQLITE_OK) 
      columnExists = YES; 
    } 
    return columnExists; 
} 
相關問題