CodeGuy
CodeGuy

Reputation: 28905

SQLite3 update not working

I'm trying to perform an update command to my sqlite3 table. But it's not working:

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
                         [c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
        const char * sql = [cmd UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"updateContact SUCCESS - executed command %@",cmd);
        }
        else {
            NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
        }

        sqlite3_finalize(compiledStatement);

    }
    else {
        NSLog(@"pdateContact FAILED - failed to open database");
    }

    sqlite3_close(database);

    NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

I see "updateContact SUCCESS - executed command ..." printed. But the table is not being updated. What am I doing wrong?

Upvotes: 2

Views: 3405

Answers (3)

Jaspreet Singh
Jaspreet Singh

Reputation: 1180

this may help you...

  +(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription 

    {
    if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
    sqlite3_stmt *compiledStmt;
    NSString  *sqlStmt=[NSString stringWithFormat:@"UPDATE contacts SET first =?, last =? , briefDescription =?, briefDescription =? WHERE first=?,last=?,briefDescription=?;"];

    if(sqlite3_prepare_v2(myDatabase, [sqlStmt UTF8String],-1,&compiledStmt, NULL)==SQLITE_OK)
    {

         NSLog(@"updateding......cycle");
    sqlite3_bind_text(compiledStmt,1, [[c first] UTF8String],-1,SQLITE_TRANSIENT);
        sqlite3_bind_text(compiledStmt,2, [[c last] UTF8String],-1,SQLITE_TRANSIENT);

     sqlite3_bind_text(compiledStmt,3, [[c briefDescription] UTF8String],-1,SQLITE_TRANSIENT);  

   sqlite3_bind_text(compiledStmt,4, [originalFirst UTF8String],-1,SQLITE_TRANSIENT);

   sqlite3_bind_text(compiledStmt,5, [originalLast UTF8String],-1,SQLITE_TRANSIENT);

    sqlite3_bind_text(compiledStmt,6, [originalBriefDescription UTF8String],-1,SQLITE_TRANSIENT);
    }
    sqlite3_step(compiledStmt);
    sqlite3_close(database);
}



  }

Upvotes: 2

xianritchie
xianritchie

Reputation: 331

A few things:

Where is your databasePath pointing? Be sure it is pointing to the documents path in the bundle, and not an external db in the project directly. Also, ensure you put the step statement correctly.

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
                     [c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
    const char * sql = [cmd UTF8String];
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
        sqlite3_step(compiledStatement); // Here is the added step.
        NSLog(@"updateContact SUCCESS - executed command %@",cmd);
    }
    else {
        NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
    }

    sqlite3_finalize(compiledStatement);

}
else {
    NSLog(@"pdateContact FAILED - failed to open database");
}

    sqlite3_close(database);
    NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

Also, I see from the line [SQLMaster getContactsFromDatabase] that you are calling other database methods. Be sure that sqlite3_finalize is being reached on all of these database calls, or the database may not release the busy handler.

Upvotes: 2

ribram
ribram

Reputation: 2460

You prepared the statement but did not execute it. Try int sqlite3_step(sqlite3_stmt*); after the prepare.

Upvotes: 1

Related Questions