SQLite-Datenbank-Update

  • Ich verwende den folgenden Code zum Aktualisieren eines Eintrags in meiner Tabelle in sqlite. Wenn Sie die anderen Spalten nicht stören, müssen Sie nur die Waage bearbeiten. Aber es gibt mir keine Änderung in der Datenbank. Kann mir hier jemand helfen ...

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.sqlite"];
    
    sqlite3 *database;
    sqlite3_stmt *updateStmt;
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
     {
        const char *sql = "update Account Set currentBalance = ? Where ID = ?";
        if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
    }
    sqlite3_bind_int(updateStmt, 1, 1000);
    
    sqlite3_bind_int(updateStmt, 2 , 1);
    sqlite3_finalize(updateStmt);
    
    
        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
    sqlite3_finalize(updateStmt);
    
    
    sqlite3_close(database);
     
    24 August 2011
    Hadi
1 answer
  • DO this ...

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.sqlite"];
    //NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Accounts.sqlite"];
    sqlite3 *database;
    sqlite3_stmt *updateStmt;
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "update Account Set currentBalance = ? Where ID = ?";
        if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
            NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
    }
    sqlite3_bind_int(updateStmt, 1, 1000);
    
    sqlite3_bind_int(updateStmt, 2 , 1);
    
    
    char* errmsg;
    sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
    
    if(SQLITE_DONE != sqlite3_step(updateStmt))
        NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
    sqlite3_finalize(updateStmt);
    
    
    sqlite3_close(database);
     
    24 August 2011
    Hadi