【发布时间】:2013-12-22 11:15:41
【问题描述】:
我正在尝试只更新一行中的一个单元格,但我无法让它工作。更新方法:
- (void) UpdateQuestionShownParameter:(int)QuestionId :(BOOL)QuestionShown{
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Milionar.sqlite"];
const char *sql = "UPDATE Questions set Show = ? WHERE id = ?";
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) == SQLITE_OK)
{
NSInteger shownInteger = (QuestionShown ? 1 : 0);
sqlite3_bind_int(sqlStatement, 1, shownInteger);
sqlite3_bind_int(sqlStatement, 2, QuestionId);
if (sqlite3_step(sqlStatement) != SQLITE_DONE)
{
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(db));
}
sqlite3_finalize(sqlStatement);
}
else
{
NSLog(@"Problem with prepare statement");
}
}
else
{
NSLog(@"An error has occured while opening database.");
}
sqlite3_close(db);
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
}
在 ViewDidLoad 中尝试:
- (void)viewDidLoad
{
ListOfQuestions *listQuestions =[[ListOfQuestions alloc] init];
self.Questions = [listQuestions getQuestions];
Question *generatedQuestion = (Question *) [self.Questions objectAtIndex:0];
[listQuestions UpdateQuestionShownParameter:generatedQuestion.id :TRUE];
[self.Description setText:(generatedQuestion.Description)];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
每当我尝试运行应用程序时,我都会在 Shown 列中得到 0。但我没有任何错误。那么,我是在做错什么,还是每次尝试在模拟器中运行应用程序时,我都会从项目数据库中重新创建数据库? 谢谢
【问题讨论】:
标签: ios iphone sqlite sql-update