检索数据时的数据库崩溃问题 [英] Database crash issue at the time of retrieving data

查看:28
本文介绍了检索数据时的数据库崩溃问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是数据库字段值.

下面是我用来从sqlite数据库中获取数据的代码

Below is the code which I am applying to fetch data from sqlite database

-(void)readDataFromRestaurantTable
{
    [self openDataBase];

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, & database) == SQLITE_OK)
    {
        sqlite3_stmt  *statement = NULL;

        NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM RestaurantDB"];
        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {

            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                strRestaurantAddress = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                strRestaurantPhone = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
                strRestaurantLatitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
                strRestaurantLongitude = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];

                NSLog(@"strRestaurantName :--> %@",strRestaurantName);
            }
        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(database);
}

在前面的代码块中应用程序没有进去

In the previous block of code application is not going inside the

if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{

}

这里得到 statement = 0x00000000 &我认为正是因为这个原因,它没有进入 if 块.

Here it is getting statement = 0x00000000 & I think because of this reason only it is not going into the if block.

崩溃时的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
First throw call stack:
(0x18dc012 0x12a1e7e 0x18dbdeb 0xcbdce6 0x78e4 0x7bdf 0x2ca817 0x2ca882 0x2ebed9 0x2ebd14 0x2ea1ea 0x2ea06c 0x2ebc57 0x12b5705 0x1ec920 0x1ec8b8 0x40f0b4 0x12b5705 0x1ec920 0x1ec8b8 0x2ad671 0x2adbcf 0x2ad6a6 0x412bb9 0x12b5705 0x1ec920 0x1ec8b8 0x2ad671 0x2adbcf 0x2acd38 0x21c33f 0x21c552 0x1fa3aa 0x1ebcf8 0x1da5df9 0x185ff3f 0x185f96f 0x1882734 0x1881f44 0x1881e1b 0x1da47e3 0x1da4668 0x1e965c 0x2712 0x2645 0x1)
libc++abi.dylib: terminate called throwing an exception

请指导我真正的问题在哪里.

Please guide me where is the actual problem lies.

推荐答案

这是由于尝试使用 NULL 字符串创建 NSString 对象而导致的.它位于以下行之一:

This is being caused by trying to create a NSString object with a NULL string. It is on one of these lines:

[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, ...)];

因此,在使用 sql 语句的结果创建 NSString 之前,您需要像这样检查 NULL:

So, before you create a NSString with the results of the sql statement you need to check for NULL like this:

char *tmp = sqlite3_column_text(statement, 1);
if (tmp == NULL)
    strRestaurantName = nil;
else
    strRestaurantName = [[NSString alloc] initWithUTF8String:tmp];

这篇关于检索数据时的数据库崩溃问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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