如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因 [英] How to call sqlite3_errmsg to know why sqlite3_prepare_v2 is failing

查看:77
本文介绍了如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于 C 的函数 sqlite3_prepare_v2 返回 1.我只想知道可读形式的错误消息并更正我的代码.我是从 raywinderlich 博客中作为教程学习的.我遇到了 sqlite3_errmsg 并且我不知道如何使用 sqlite3_errmsg 函数.

C based function sqlite3_prepare_v2 is returning 1. I simply want to know the error message in readable form and to correct my code. I was learning this as a tutorial from raywinderlich blogs. I have come across sqlite3_errmsg and I have no idea how to use sqlite3_errmsg function.

虽然在此处问了同样的问题,但遗憾的是仍未得到答复.

Although same question has been asked here, unfortunately still is unanswered.

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

I wanna know the error and correction will highly be appreciated. thanks.

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

sqlite3_errmsg() 和 sqlite3_errmsg16() 返回英文描述错误的文本,分别为 UTF-8 或 UTF-16.保存错误消息字符串的内存在内部进行管理.这应用程序不需要担心释放结果.然而,错误字符串可能会被后续操作覆盖或解除分配调用其他 SQLite 接口函数.

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.

sqlite3_errstr() 接口返回英文文本将结果代码描述为 UTF-8.保存错误信息的内存字符串由内部管理,不得由应用程序释放.

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 错误消息

这篇关于如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆