Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS SQLite Update issue
    primarykey
    data
    text
    <p>After three days trying, I came here to get som help. I want to Update multiple columns in my SQLite database in my app. So i Have a "for" to call the update method many times as needed. But only the first row is affected.</p> <p>My for loop looks like:</p> <pre><code>for (NSDictionary *friend in friends) { NSMutableArray *valuesForUpdate = [[NSMutableArray alloc] init]; DBHelper *dbHelper = [[DBHelper alloc] initWithPath:@"DB_CodiUp"]; [valuesForUpdate addObject:[friend objectForKey:@"name"]]; [valuesForUpdate addObject:[friend objectForKey:@"code"]]; [valuesForUpdate addObject:[friend objectForKey:@"store"]]; [valuesForUpdate addObject:[friend objectForKey:@"imgData"]]; [dbHelper updateColumns:columnsToUpdate inTable:tableName withValues:valuesForUpdate where:@"id" isIqualsTo:[friend objectForKey:@"id"]]; } </code></pre> <p>And my Update method is this:</p> <pre><code>- (BOOL) updateColumns:(NSArray *)columns inTable:(NSString *)tableName withValues:(NSArray *)values where:(NSString *)columnSelector isIqualsTo:(NSString *)selector { sqlite3_stmt *statement; const char *sql; NSString *preSQL = [NSString stringWithFormat:@"Update %@ set ",tableName]; //Build sql for(int i = 0; i &lt; [columns count]; i++){ if(i == 0) preSQL = [NSString stringWithFormat:@"%@ %@ = ?", preSQL,[columns objectAtIndex:i]]; else preSQL = [NSString stringWithFormat:@"%@ , %@ = ?", preSQL,[columns objectAtIndex:i]]; } preSQL = [NSString stringWithFormat:@"%@ where %@ = %@", preSQL, columnSelector, selector]; sql = [preSQL UTF8String]; //Prepare statement if(sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL) == SQLITE_OK) { for(int i = 0; i &lt; [values count]; i++){ if([[values objectAtIndex:i] isKindOfClass:[NSString class]]) { sqlite3_bind_text(statement,i+1,[[values objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT); } else if([[values objectAtIndex:i] isKindOfClass:[NSNumber class]]) { sqlite3_bind_int(statement, i+1, ((NSNumber *)[values objectAtIndex:i]).integerValue); } else if ([[values objectAtIndex:i] isKindOfClass:[NSData class]]) { sqlite3_bind_blob(statement, i+1, [[values objectAtIndex:i] bytes], [[values objectAtIndex:i] length], SQLITE_TRANSIENT); } else { NSLog(@"[SQLITE UPDATE] ERROR! The value at index %d is not a recognized data type", i); } } //Execute statement int success; success = sqlite3_step(statement); if (success != SQLITE_DONE) { NSLog(@"Error on step: %i",sqlite3_errcode(database)); } sqlite3_finalize(statement); NSLog(@"[DataBase]Updated row"); sqlite3_close(database); return YES; } else { NSAssert1(0,@"Error: Failed to prepare statement with message %s '.", sqlite3_errmsg(database)); return NO; } } </code></pre> <p>The method throws no error, it says that everything is normal and Updates has been executed. But when I do a SELECT * to that table, only the first row has been affected. Please, please, please take a look to the code and tell me what am doing wrong.</p> <p>THANKS!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload