jeudi 7 janvier 2016

FMDB insert query doesn't have any effect

Problem:

I'm trying to insert data into my database. As far as I know I've copied the database successful from the main bundle. But I don't get insert-queries to work. Select statements work without a problem.

No error messages were shown.

The strange thing however is that if I run the query using SQLiteBrowser on the database in the main bundle everything works fine.

Code:

- (id)init {
    /* Initializes and opens the database. */
    if (self = [super init]) {
        NSString *managerPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        managerPath = [managerPath stringByAppendingString:@"EKCalendar.db"];
        database = [FMDatabase databaseWithPath:managerPath];
        [database open];
    }
    return self;
}

-(void)addCourseArray:(NSArray *)courseArray toCustomsForLevelId:(int)levelId {
    [database beginTransaction];
    for (NSString *courseName in courseArray) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO EKCustom (CourseId) SELECT ID FROM EKCourses WHERE CourseName = 'courseName' AND LevelId = 'levelId'"];
        BOOL success = [database executeUpdate:sql];
        if (!success) {
            NSLog(@"error = %@", [database lastErrorMessage]);
        }
    }
    [database commit];
    NSLog("Done"); /* This shows up in the debugger */
}

Variables:

courseArray is a regular NSArray containing multiple NSStrings.

levelId is a integer that can be any value between 1 and 5.


Versions:

  • Xcode 7.2

  • iOS 9.2 (Simulator)

  • FMDB 2.5 (Podfile)

Aucun commentaire:

Enregistrer un commentaire