【发布时间】:2011-10-05 02:13:12
【问题描述】:
我在更新数据时遇到问题。我使用此代码进行更新。
这一切进展顺利,但我的数据没有更新。这有什么问题?
我想更新 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);
}
【问题讨论】:
-
数据库中的 JournyID 是否为 Text 数据类型?
-
没有什么可以绑定的,因为你没有使用参数化查询,那你为什么要绑定 sqlite3_bind_text?
-
@jignesh NO JourneyID 是 VARCHARPRIMARY KEY
-
尝试评论这一行 sqlite3_bind_text(compiledStatement, 1, [str UTF8String], -1, SQLITE_TRANSIENT);
-
No It's not working my firend.Ok 你的意思是说在更新语句中我们不能使用绑定行是正确的
标签: iphone objective-c sqlite