Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:35:52+00:00 2026-06-01T06:35:52+00:00

I want to alter a table if that column already exists. If it doesn’t

  • 0

I want to alter a table if that column already exists. If it doesn’t I want to add it.

My code :

-(void) alterDB{
    sqlite3_stmt *statement; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
    
        NSString *updateSQL = [NSString stringWithFormat: @"ALTER TABLE diagramInfo ADD COLUMN testColumn VARCHAR"];
        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 release];
            alert=nil;
        
        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Updation" message:@"DB not Altered" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            [alert release];
            alert=nil;
        }   
        // Release the compiled statement from memory
        sqlite3_finalize(statement);    
        sqlite3_close(database);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:35:53+00:00Added an answer on June 1, 2026 at 6:35 am

    Use following function for checking if column exists. Not checked practically so you may have to check syntax. Concept is like if you are able to create prepared statement than column exists otherwise not.

    -(BOOL)checkColumnExists
    {
        BOOL columnExists = NO;
    
        sqlite3_stmt *selectStmt;
    
        const char *sqlStatement = "select yourcolumnname from yourtable";
        if(sqlite3_prepare_v2(yourDbHandle, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK)
            columnExists = YES;
    
        return columnExists;
    }
    

    Swift 3.2:

    private func tableHasColumn(db: OpaquePointer, tableName: String, columnName: String) -> Bool {
    
            var retVal = false
    
            var tableColumnsQueryStatement: OpaquePointer? = nil
            if sqlite3_prepare_v2(db, "PRAGMA table_info(\(tableName));",
                                -1,
                                &tableColumnsQueryStatement,
                                nil) == SQLITE_OK {
    
                while (sqlite3_step(tableColumnsQueryStatement) == SQLITE_ROW) {
    
                    let queryResultCol1 = sqlite3_column_text(tableColumnsQueryStatement, 1)
                    let currentColumnName = String(cString: queryResultCol1!)
    
                    if currentColumnName == columnName {
                        retVal = true
                        break
                    }
                }
            }
    
            return retVal
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to alter a table and add a column that is the sum
Suppose that I have an enum column on a table and I want users
I want to create a persisted computed column in a table so that I
I want to alter a table from INNODB to MEMORY ENGINE. So I typed
Basically I want to alter the boolean value selecting from the table: e.g.: SELECT
I want to display the column 'name' after being found from the table mytest.
We have a MySQL table that was altered with ALTER TABLE t COMMENT 'foo'
I have a table column that needs to be limited to a certain width
I have a table that has an identity column as the Primary Key. None
How can I add a column in an SQLite table if and only if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.