FMDB: add new column and insert data
Asked Answered
H

3

9

In my app I'm using FMDatabase. I'm trying to add new column to existing table and then insert data in that column. When I added the column by Alter Table, I got no errors but when I'm trying to insert data in the table, I got error complaining about the newly added column that doesn't exist.

Here is my code

NSString *databasePath = [myDB getPlacesDBPath];

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

if (![db open])
{
    return ;
}

if (![db columnExists:@"placeName" inTableWithName:@"MYPLACES"])
{
    [db executeQuery:@"ALTER TABLE MYPLACES ADD COLUMN placeName TEXT"];
    // I tried to set the result of this query in "BOOL Success" and print the results and I got YES    
}

//Note: I have more than 12 columns but I didn't post all of them here
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO  MYPLACES  ( placeID ,  placeName)  VALUES (\"%@\", \"%@\")", placeIDValue, placeNameValue ];

[db executeUpdate:insertSQL];

if ([db hadError]) {
    NSLog(@"error : %@",[db lastError]);
}

[db close];

I got the following error:

error : Error Domain=FMDatabase Code=1 "table MYPLACES has no column named placeName" UserInfo=0xe340920 {NSLocalizedDescription=table MYPLACES has no column named placeName}

What's the problem? how to know why the new column is not added?

Hoist answered 1/1, 2014 at 15:32 Comment(0)
M
16

I see nothing in the above simplified code sample to explain the error you report. The problem may rest elsewhere (or, perhaps in the process of simplifying the code sample, you eliminated the source of the problem).

That having been said, I had a couple of reactions:

  1. You said:

    I tried to set the result of this query in "BOOL Success" and print the results and I got YES

    I don't know how that's possible, because executeQuery returns a FMResultSet not a BOOL. This is moot, though, because your ALTER TABLE statement should be performed with executeUpdate not executeQuery. (The latter should still work, but the former is more logical.)

  2. I don't understand why you removed the error checking. That's the best way to identify issues.

  3. Completely unrelated, but you should never use stringWithFormat when building your SQL. You open yourself up to SQL failures resulting from unexpected input (e.g. what if one of your values had double quotes inside it). Worse, you expose yourself to SQL injection attacks. Instead, use ? placeholders in the SQL and then just pass the values as parameters to the executeUpdate (or executeQuery) method.

So, the following code worked fine for me:

BOOL success;

FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

if (![db open])
{
    NSLog(@"open failed");
    return;
}

if (![db columnExists:@"placeName" inTableWithName:@"MYPLACES"])
{
    success = [db executeUpdate:@"ALTER TABLE MYPLACES ADD COLUMN placeName TEXT"];
    NSAssert(success, @"alter table failed: %@", [db lastErrorMessage]);
}

NSString *insertSQL = @"INSERT INTO  MYPLACES  (placeID, placeName)  VALUES (?, ?)";
success = [db executeUpdate:insertSQL, placeIDValue, placeNameValue];
NSAssert(success, @"insert failed: %@", [db lastErrorMessage]);

[db close];

Since I don't see what would cause the problem you describe, I can only suggest you single-step through this code in your debugger and make sure it's following the path you think it is.

Mariahmariam answered 1/1, 2014 at 17:25 Comment(1)
Thanks for your clear, organised, and simple answer. I don't know what was the problem exactly but using executeUpdate instead of executeQuery solved the problem.Hoist
T
2

Swift 3 code :

 let db = FMDatabase(path: Util.getPath(databasePath)
    guard db != nil else { return }

    db!.open()
    if !(db!.columnExists("tableName", columnName: "USERID")) {

        let alterTable = "ALTER TABLE tableName ADD COLUMN USERID TEXT"
        if db!.executeUpdate(alterTable, withArgumentsIn: nil) {
            printf(“new column added”)
    }
    else {
        printf(“issue in operation”)
    }
    db!.close()

May be help someone. Keep coding..

Titivate answered 21/12, 2017 at 13:11 Comment(0)
S
0

The problem is that -[FMDatabase executeQuery:] does not execute submitted instructions, it only prepares the statement.

To execute them you need to perform a "step".

In FMDB that is done by -[FMResultSet next], internally - by call to sqlite3_step( ).

Since -executeUpdate: (in contrast to -executeQuery:) does that for you and finalizes the statement - it may be more appropriate for this purpose.

So if you really need to use -executeQuery:

FMResultSet *rs = [db executeQuery:@"ALTER TABLE MYPLACES ADD COLUMN placeName TEXT"];
[rs nextWithError:&error]; // error handling is recommended here
[rs close];

But -executeUpdate: is more concise

BOOL success = [db executeUpdate:@"ALTER TABLE MYPLACES ADD COLUMN placeName TEXT"];
Selfrighteous answered 4/7, 2018 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.