在sqlite for iPhone中获取更新语句的问题

时间:2011-07-14 06:52:26

标签: iphone objective-c sqlite

我在更新数据方面遇到了问题。我使用此代码进行更新。

这一切进展顺利但我的数据没有得到更新。这有什么问题?

我想更新EndDate列值:它的数据类型是DATETIME。

所有事情都执行得很好但是我的数据库表UserJourney的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 :(得分:0)

尝试替换以下方法。我做了几个改变。它可能有所帮助。如果不行,您可以发布数据库表列数据类型吗?

-(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);    

}

希望得到这个帮助。

答案 1 :(得分:0)

你必须使用updateStatement而不是compileStatement