Проблема в получении обновления оператора, стреляющего в sqlite для iPhone - PullRequest
3 голосов
/ 14 июля 2011

У меня проблема с обновлением данных. Я использую этот код для обновления.

Все идет хорошо, но мои данные не обновляются. в чем проблема в этом?

Я бы хотел обновить значение столбца EndDate: его тип данных - DATETIME.

Все выполняется очень хорошо, но моя таблица базы данных UserDourney EndDate не получает обновления В чем проблема и как я могу ее решить?

-(void)journeyEnd
{

    NSString *filePath = [self getWritableDBPath];

    sqlite3 *database;


    NSDate* date = [NSDate date];
    //Create the dateformatter object
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    //Set the required date format
    [formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
    //Get the End date
    NSString* str = [formatter stringFromDate:date];
    NSLog(@"this from new map '%@'",journeyid);

    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {

**this line where i am trying to insert statement after that i remember that i have fire the UPDATE statement** 
        //const char *sqlStatement = "insert into UserJourney(EndDate) VALUES (?) where JourneyID = ''";
//And here I fire the Update statement 
        NSString *sqlStatement = [NSString stringWithFormat:@"UPDATE UserJourney SET EndDate='%@' WHERE JourneyID='%@'",str,journeyid]; 
            sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, [sqlStatement UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK)    {

            sqlite3_bind_text( compiledStatement, 1, [str UTF8String], -1, SQLITE_TRANSIENT);

        }
        if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
            NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
        }
        else {
            sqlite3_reset(compiledStatement);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            alert = nil;

        }

        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);    

}

Ответы [ 2 ]

0 голосов
/ 13 сентября 2011

вы должны использовать updateStatement not compiledStatement

0 голосов
/ 14 июля 2011

Попробуйте, заменив ниже метод.Я сделал пару изменений.Это может помочь.Если не работает, вы можете опубликовать типы данных столбца таблицы базы данных?

-(void)journeyEnd
{

NSString *filePath = [self getWritableDBPath];

sqlite3 *database;


NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
//Get the End date
NSString* str = [formatter stringFromDate:date];
NSLog(@"this from new map '%@'",journeyid);

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {

**this line where i am trying to insert statement after that i remember that i have fire the UPDATE statement** 
    //const char *sqlStatement = "insert into UserJourney(EndDate) VALUES (?) where JourneyID = ''";
//And here I fire the Update statement 
        NSString *sqlStatement = [NSString stringWithFormat:@"UPDATE UserJourney SET EndDate='%@' WHERE JourneyID='%d'",str,journeyid]; 
        sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, [sqlStatement UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK)    {


    }
    if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
        NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
    }
    else {
        sqlite3_reset(compiledStatement);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        alert = nil;

    }

    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);    

}

Надеюсь, эта помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...