dimanche 10 janvier 2016

Sqlite select return 1 row after insert

I have a problem after select from sql. I send to updateRow an NSArray of 5 objects. After update (I did 5 times addRow), I get just one of them back on getGroupsByUserId.

Here is my code:

+(void)updateRow:(sqlite3*)database rowToUpdate:(NSArray*)rowToUpdate{
    for (UserGroup* ug in rowToUpdate) {
        [UsersGroupsRelationShipSql addRow:database userId:ug.userId groupId:ug.groupId ];
    }
}

+(void)addRow:(sqlite3*)database userId:(NSString*)userId groupId:(NSString*)groupId{
            sqlite3_stmt *statment;
            NSString* query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@,%@) values (?,?);",USER_GROUPS_TABLE,USER_ID,GROUP_ID];

            if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
                sqlite3_bind_text(statment, 1, [userId UTF8String],-1,NULL);
                sqlite3_bind_text(statment, 2, [groupId UTF8String],-1,NULL);
                if(sqlite3_step(statment) == SQLITE_DONE){
                    sqlite3_finalize(statment);
                    return;
                }
            }
            sqlite3_finalize(statment);
            NSLog(@"ERROR: addRowToTable failed %s",sqlite3_errmsg(database));
        }

+(NSMutableArray*)getGroupsByUserId:(sqlite3*)database userId:(NSString*)userId{
            NSMutableArray* data = [[NSMutableArray alloc] init];
            sqlite3_stmt *statment;
            NSString* query = [NSString stringWithFormat:@"SELECT * from USERS_GROUPS WHERE %@ = %@;", USER_ID,userId];

            if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
                while(sqlite3_step(statment) == SQLITE_ROW){
                    NSString* usId = [NSString stringWithFormat:@"%s",sqlite3_column_text(statment,0)];
                    NSString* grId = [NSString stringWithFormat:@"%s",sqlite3_column_text(statment,1)];

                    UserGroup* ug = [[UserGroup alloc] init:usId groupId:grId];
                    [data addObject:ug];
                }
            }
            else{
                NSLog(@"ERROR: getGroupsByUserId failed %s",sqlite3_errmsg(database));
                sqlite3_finalize(statment);
                return nil;
            }
            sqlite3_finalize(statment);
            return data;
        }

Aucun commentaire:

Enregistrer un commentaire