I'm having a problem in updating data. I use this code for the update.
All this is going well but my data not get updated. what's the problem in this?
I'd like to update the EndDate column value: its data type is DATETIME.
All thing execute very well but my database table UserJourney's EndDate not getting update What's the problem and how can I solve it?
-(void)journeyEnd
{
NSString *filePath = [self getWritableDBPath];
sqlite3 *d开发者_JAVA技巧atabase;
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);
}
Try by replacing below method. I did couple of changes. It might help. If not work can you post your database table column datatypes ?
-(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);
}
Hope this help.
you have to use updateStatement not compiledStatement
精彩评论