How to call sqlite3_errmsg to find out why sqlite3_prepare_v2 is not working

A C-based function sqlite3_prepare_v2returns 1. I just want to know the error message in readable form and correct my code. I studied this as a tutorial from Raywinderlich blogs. I came across sqlite3_errmsgand I have no idea how to use the function sqlite3_errmsg.

Although the same question was asked here , unfortunately, it still remains unanswered.

I want to know that the error and correction will be highly appreciated. thank.

- (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;

}
+4
source share
1 answer

You can use sqlite3_errmsg()as:

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

Also you can use sqlite3_errstr().

sqlite3_errmsg() sqlite3_errmsg16() , , UTF-8 UTF-16 . . , . , SQLite.

sqlite3_errstr() , , UTF-8. .

SQLite

+5

Source: https://habr.com/ru/post/1546953/


All Articles