Translate

Wednesday, April 4, 2012

Update Database

//#import <sqlite3.h>
//sqlite3 *database;


-(void)updateDatabase:(NSString*)bookTitle1 andauthor:(NSString*)bookAuthorName1 andid:(NSString*)bookId1{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
    NSString *libraryDirectory=[paths objectAtIndex:0];
    NSString *databasepath1 = [libraryDirectory stringByAppendingPathComponent:@"database"];
   
    if(sqlite3_open([databasepath1 UTF8String], &database) == SQLITE_OK) {
        sqlite3_stmt *compiledStatement;
        const char *sql="UPDATE tbl_book Set fld_title=?,fld_author=? Where fld_id=?";          
        if (sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK){
           
            NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
            if (SQLITE_DONE != sqlite3_step(compiledStatement))
                NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
           
        }
        sqlite3_reset(compiledStatement);
        sqlite3_bind_text(compiledStatement, 1, [bookTitle1 UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(compiledStatement, 2, [bookAuthorName1 UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_int(compiledStatement, 3, [bookId1 intValue]);
       
        int success = sqlite3_step(compiledStatement);
        if (success == SQLITE_ERROR) {
            NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
        }
        else{
            NSLog(@"Success");
            sqlite3_finalize(compiledStatement);
        }
        sqlite3_close(database);
    }
   
}

No comments:

Post a Comment