ios - 如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因

标签 ios objective-c xcode sqlite

基于 C 的函数 sqlite3_prepare_v2 返回 1。我只是想知道可读形式的错误消息并更正我的代码。我是从 raywinderlich 博客上学习这个教程的。我遇到过 sqlite3_errmsg 但我不知道如何使用 sqlite3_errmsg 函数。

尽管有人问过同样的问题here , 不幸的是仍然没有答案。

我想知道错误和更正将不胜感激。谢谢。

- (NSArray *)failedBankInfos {
    NSMutableArray *retval = [[NSMutableArray alloc]init];
    NSString *query = @"SELECT id, name, city, state FROM failed_banks ORDER BY close_date DESC";
    sqlite3_stmt *statement;
    int tmp  = sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil);
    NSLog(@"%i",tmp); // printing 1
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            int uniqueId = sqlite3_column_int(statement, 0);
            char *nameChars = (char *) sqlite3_column_text(statement, 1);
            char *cityChars = (char *) sqlite3_column_text(statement, 2);
            char *stateChars = (char *) sqlite3_column_text(statement, 3);

            NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
            NSString *city = [[NSString alloc] initWithUTF8String:cityChars];
            NSString *state = [[NSString alloc] initWithUTF8String:stateChars];
            NSLog(@"name is  : %@",name);
             NSLog(@"city is : %@",city);
            FailedBankInfo *info = [[FailedBankInfo alloc]
                                    initWithUniqueId:uniqueId name:name city:city state:state];
            [retval addObject:info];
        }
        sqlite3_finalize(statement);
    }
    else
    {
        // if part is failing and control is arriving in else.
    }
    return retval;

}

最佳答案

您可以像这样使用 sqlite3_errmsg():

NSLog(@"Database Error Message : %s", sqlite3_errmsg(_database));

你也可以使用 sqlite3_errstr()

The sqlite3_errmsg() and sqlite3_errmsg16() return English-language text that describes the error, as either UTF-8 or UTF-16 respectively. Memory to hold the error message string is managed internally. The application does not need to worry about freeing the result. However, the error string might be overwritten or deallocated by subsequent calls to other SQLite interface functions.

The sqlite3_errstr() interface returns the English-language text that describes the result code, as UTF-8. Memory to hold the error message string is managed internally and must not be freed by the application.

引用 SQLite Error Messages

关于ios - 如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546711/

相关文章:

ios - 录制视频并上传到 Firebase

ios - 使用 gem 安装 cocoapods 时出错

ios - 添加 UIGestureRecognizer 以从左向右滑动我的 View

iphone - 如何在单击按钮时弹出默认的 iPhone 键盘

ios - 使用 2 个排序描述符对数组进行排序

objective-c - COCOA 中的 JSON(使用 YAJL)

c - Xcode C 代码, "division by zero"分析器问题

iphone - 带有 sectionsNameKeyPath 和结果顺序的 NSFetchResultsController

iphone - iOS sdk 的最佳绘图库

ios - 如何通过 objective-c 获取这样的 internet 请求信息