gpt4 book ai didi

ios - sqlite 中的更新行未更新

转载 作者:行者123 更新时间:2023-12-02 07:26:20 25 4
gpt4 key购买 nike

我试图只更新一行中的一个单元格,但无法使其工作。更新方法:

- (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.
}

每次我尝试运行应用程序时,我都会在显示列中得到 0。但我没有任何错误。那么我是否做错了什么,或者每次当我尝试在模拟器中运行应用程序时,我都会从项目数据库重新创建数据库?谢谢

最佳答案

您正在打开 bundle 中的数据库,该数据库是只读的。如果文档文件夹中尚不存在该数据库,您应该将数据库从 bundle 复制到文档文件夹:

NSString *filename         = @"Milionar.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:filename];
NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:filename];

if (![fileManager fileExistsAtPath:documentsPath]) {
NSError *error = nil;
BOOL success = [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error];
NSAssert(success, @"Unable to copy database: %@", error);
}

if (sqlite3_open([documentsPath UTF8String], &db) != SQLITE_OK) {
NSLog(@"Open failed");
} else {
// ...
}

有关文档所属位置的更多信息,请参阅 File System Programming Guide .

<小时/>

顺便说一句,如果您正在寻找模拟器的 Documents 文件夹,该文件夹位于 ~/Library/Application Support/iPhone Simulator 中(在 Xcode 6 中,现在是 ~/Library/Developer/CoreSimulator/Devices)。如果您没有看到“Library”文件夹,可以通过在终端命令行界面中键入以下命令来取消隐藏它:

chflags nohidden ~/Library

关于ios - sqlite 中的更新行未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com