0

So I can't seem to figure out why my database is not updating with my insert query, could someone please check out my code and let me know if they see anything? Thanks so much in advanced.

-(void)connectSQL {
@try {
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* dbPath = [documentsPath stringByAppendingPathComponent:@"UserDB1.sqlite"];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];

    if (!fileExists) {
        NSLog(@"Didn't find database");
        NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"UserDB1.sqlite"];
        //Here we copy our database to documents located on the users phone.
        [[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil];
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
    }
    queryString = @"INSERT INTO FavoriteExercises ('ExerciseID','ExerciseType') VALUES ('1','strength')";
    const char *sql = [queryString cStringUsingEncoding:NSASCIIStringEncoding];
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);
}
}

Thanks to anyone who tries to help.

Robert Saunders
  • 403
  • 8
  • 22
  • The very first thing to check: Is the file really where you think it is, and are you opening it in read/write space? About 75% if iOS SQLite newbies screw up with the directory stuff, one way or another. – Hot Licks Jul 09 '14 at 02:57
  • 2
    Of course, in your case you never actually execute the prepared statement. That might have something to do with your problem. – Hot Licks Jul 09 '14 at 02:59
  • 2
    Get rid of the try/catch code and do proper error checking of all `sqlite3` functions. – rmaddy Jul 09 '14 at 03:35
  • 1
    To further what Hot Licks states - where's your call to `sqlite3_step`? – rmaddy Jul 09 '14 at 03:37

0 Answers0