lundi 8 juin 2015

Update not working sqlite in objective c

I am trying to update row in a loop and its not working. I have explained the scenario below

I have got set of values from web services stored in arrays. Now i have to update those values in DB by looping through the array. Below is the code am using:

NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"db_name" ofType:@"sqlite"];

sqlite3 *database;

sqlite3 *database1;

sqlite3_open([dbPath UTF8String], &database1);

sqlite3_open([dbPath UTF8String], &database);    

NSInteger loopCnt = 0;

NSString *s = [[NSString alloc] init];

const char *sql;

sqlite3_stmt *selectstmt;

NSString *q = [[NSString alloc] init];

for(NSString *itemId in itemIds)
{

    s = [NSString stringWithFormat:@"SELECT * from table where id=%@", itemId];

    sql = [s UTF8String];

    int result = sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL);

    if(result == SQLITE_OK)
    {
        while(sqlite3_step(selectstmt) == SQLITE_ROW)
        {
            if ((char *)sqlite3_column_text(selectstmt, 0) != NULL)
            {
                rowNumVal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];

                col1 = [values1 objectAtIndex:loopCnt];

                col2 = [values2 objectAtIndex:loopCnt];

                if([rowNumVal integerValue] > 0)
                {
                    q = [NSString stringWithFormat:@"UPDATE table set col1=?, col2=? WHERE id=?"];
                }
            }
        }
    }        
    sqlite3_finalize(selectstmt);        

    sqlite3_exec(database1, "BEGIN EXCLUSIVE TRANSACTION", 0, 0, 0);

    sqlite3_stmt *stmt;

    const char *query = [q UTF8String];

    if(sqlite3_prepare_v2(database1, query, -1, &stmt, NULL)== SQLITE_OK)
    {

        if(sqlite3_bind_text(stmt, 1, [col1  UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK)
        {
            NSLog(@"Error while binding column 1 %s", sqlite3_errmsg(database1));
        }

        if(sqlite3_bind_text(stmt, 2, [col2 UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK)
        {
            NSLog(@"Error while binding column 2 %s", sqlite3_errmsg(database1));
        }

        if (sqlite3_step(stmt) != SQLITE_DONE){
            NSLog(@"Delete commit failed. Error %s", sqlite3_errmsg(database1));
        }
        if(sqlite3_reset(stmt)!= SQLITE_OK){
            NSLog(@"SQL error %s", sqlite3_errmsg(database1));
        }            
    }        

    sqlite3_trace(database1, sqliteCallbackFunc, NULL);

    sqlite3_finalize(stmt);

    if(sqlite3_exec(database1, "COMMIT TRANSACTION", 0, 0, 0) != SQLITE_OK){
        NSLog(@"SQL error %s", sqlite3_errmsg(database1));
    }

    loopCnt++;
}

sqlite3_close(database);

sqlite3_close(database1);

I am fetching the values using id to check row exists and if exists am updating. Everything logs and runs fine but still DB is not getting updated.

Please let me know where is the issue.

Aucun commentaire:

Enregistrer un commentaire