Search code examples
objective-csqlitesql-delete

Delete column from Sqlite database


Basically I have to enter all textfield values in database then have to use for sending to webservice. So when the one column details send to service then have to delete that column. I have done this:

-(void)deleteTableDataFromSavedDataTable:(NSString *)lastID {
    NSString *sql_str=[NSString stringWithFormat:@"DELETE FROM FormInfoValues where Phone = %@",lastID];

    const char *sql = [sql_str UTF8String];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        NSLog(@"sql delete statement is ");

        sqlite3_stmt *deleteStmt;
        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) == SQLITE_OK)
        {
            NSLog(@"sql delete statement is %@", deleteStmt);
            if(sqlite3_step(deleteStmt) != SQLITE_DONE )
            {
                NSLog( @"Error: %s", sqlite3_errmsg(database) );
            }
            else
            {
                NSLog( @"row id = %lld", (sqlite3_last_insert_rowid(database)+1));
                NSLog(@"No Error");
            }
        }
        sqlite3_finalize(deleteStmt);
    }
    sqlite3_close(database);
}

but its not deleting after sending. Why its not calling? give me ideas..


Solution

  • It's quite possible I'm not understanding your issue properly -- so I apologize in advance if so.

    Regarding: "...then have to delete that column."

    -- If you want to clear the data in a column (by setting it to zero, or empty string, or NULL) then you'll want to use the UPDATE command in SQL. The DELETE command always deletes ROWS.

    -- If you really must remove a column from a table's schema, you'll have to create a new table in sqlite. (Sqlite allows you to ADD a column via ALTER TABLE command, and some other databases DO allow you to drop a column using ALTER TABLE as well.) You can quickly copy a table (without it's constraints, etc) via e.g.:

    CREATE TABLE MyOutput as SELECT a,b,d,f,h,z from MyOriginal;

    -- If your output is created by a SELECT statement, just avoid using "*" and specify just the columns you want included.