Skip to content Skip to sidebar Skip to footer

Unable To Update Database

I'm having trouble updating my database. I believe it's due to it possibly being read only, however I am unsure. I created my database in my console, and the code below adds the da

Solution 1:

You are forgeting some code for update statement

//the binding part is missing in your code , you need to write after Prepare statement.sqlite3_bind_text(compiledStatement, 1, [string UTF8String], -1, SQLITE_TRANSIENT);

if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));

sqlite3_reset(compiledStatement);

Via iphonesdkarticles.com.

This will resolve your error.

Solution 2:

-(BOOL) someUpdateFunction {
    BOOL updatedSuccessfully = NO;

    // Open the database.if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSString *updateQuery = [[NSString alloc] initWithFormat:@"UPDATE people SET user = 'terror' WHERE user = 'greg'"];
        constchar *query = [updateQuery UTF8String];
        [updateQuery release];

        sqlite3_stmt *statement; 
        int res = (sqlite3_prepare_v2( database, query, -1, &statement, NULL));

        if(res == SQLITE_OK) {
            int result = sqlite3_step(statement);
            if(result == SQLITE_DONE) {
                updatedSuccessfully = YES;
                sqlite3_finalize(statement);
            }else {
                NSLog(@"Failed with error code: %d", result);
            }
        }else {
            NSLog(@"Failed to prepare the update query with error code: %d", res);
        }
    }else {
        NSLog(@"Failed to open the database");
    }

    sqlite3_close(database);
    //NSLog(updatedSuccessfully);//NSLog(@"The value of the bool is %@\n", updatedSuccessfully);return updatedSuccessfully;
}

I was supplied this in the chat room. Thanks lads.

Remember to copy the DB over to the device else. You can find the DB path with this code:

- (NSString *) dataFilePath:(NSString *) dbName { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbName];
    return dbPath;
}

-(BOOL)buildtheDB{
    BOOL updatedSuccessfully = NO;

    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0]; 
    NSString *sqlFile =[documentDirectory stringByAppendingPathComponent:kDatabaseFileName];

    if ([[NSFileManager defaultManager] fileExistsAtPath:sqlFile] == NO) {
        NSString *bundleSql = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"sqlite"];

        NSError *error = nil;

        BOOL databaseCopied = [[NSFileManager defaultManager] copyItemAtPath:bundleSql toPath:sqlFile error:&error];
        if (databaseCopied == NO) {
            if (error) {
                NSLog(@"Could not copy database file with error: %@", [error localizedDescription]);
            }
        }else {
            NSLog(@"Database copied successfully");
        }
    }
    else{
        NSLog(@"Database already copied");
    }

    return updatedSuccessfully;
}

Post a Comment for "Unable To Update Database"