gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:35:07 25 4
gpt4 key购买 nike

基于 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/

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