dimanche 10 janvier 2016

IOS Wrong Sqlite Select

I have a problem after adding new rows to my sqlite database.

After updateRow method I added the new rows to database and I updated the lastUpdateDate parameter for next loading. The problem is that after I get into getGroupsByUserId the second time, and the return value after adding rows is again 1 and not 3 (all the rows). I think that the rows were added to the database but I can't read them properly.

    +(NSMutableArray*)getUsersByGroupId:(sqlite3*)database groupId:(NSString*)groupId{
        NSMutableArray* data = [[NSMutableArray alloc] init];
        sqlite3_stmt *statment;
        NSString* query = [NSString stringWithFormat:@"SELECT * from USERS_GROUPS where %@ = %@;", GROUP_ID, groupId];

        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)];

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

    }

+(void)updateRow:(sqlite3*)database rowToUpdate:(NSArray*)rowToUpdate{
    for (ModelUserGroup* 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));
}

Aucun commentaire:

Enregistrer un commentaire