【问题标题】:Update row in sqlite isn`t updatingsqlite 中的更新行没有更新
【发布时间】: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


    【解决方案1】:

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

    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 ~/库

    【讨论】:

    • 正是我正在寻找的内容,并带有文章链接和很棒的提示。谢谢++
    猜你喜欢
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多