5

在此处输入图像描述 这是数据库字段值。

下面是我申请从 sqlite 数据库中获取数据的代码

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

在前面的代码块应用程序里面是不走的

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

}

在这里,它得到了statement = 0x00000000 & 我认为正因为如此,它才不会进入 if 块。

崩溃时的错误

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

请指导我实际问题出在哪里。

4

5 回答 5

8

这是由尝试使用NULL字符串创建 NSString 对象引起的。它位于以下行之一:

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

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

char *tmp = sqlite3_column_text(statement, 1);
if (tmp == NULL)
    strRestaurantName = nil;
else
    strRestaurantName = [[NSString alloc] initWithUTF8String:tmp];
于 2013-01-31T05:23:42.503 回答
2

您必须检查第一个数据是否为 ​​NULL,然后像下面这样绑定它:-

const char* date = (const char*)sqlite3_column_text(statement, 3);
NSString *enddate = date == NULL ? nil : [[NSString alloc] initWithUTF8String:date];

在下面的方法中实现它:

-(void)readDataFromRestaurantTable {

    [self openDataBase];
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
     {

        const char *sql = "SELECT * FROM RestaurantDB";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
            {

                while(sqlite3_step(selectstmt) == 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)];

               }
        }
    }
    else
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}

可能对你有帮助:)

于 2013-01-31T05:30:19.143 回答
1

sqlite3_finalize(statement);在条件里面写if

 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);
 }
于 2013-01-31T06:17:17.253 回答
1

我遇到了同样的错误。
我们可以修改 SQL 查询以返回 NOT NULL/BLANK 值,而不是检查 NULL 或空白值。

在下面的查询中,针对数据库级别的空白/空值验证餐厅名称。

注意:如果有语法错误,请原谅......只考虑了 1 列。

前任:

const char *sql = "SELECT * FROM RestaurantDB WHERE <restuarant_Name column> != '' AND <restuarant_Name column> IS NOT NULL";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
            {
             while(sqlite3_step(selectstmt) == SQLITE_ROW) 
                {
                  strRestaurantName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                }
            }
于 2014-05-07T10:11:26.567 回答
1

在我的情况下,当我跳过db 表中的第一列“regno”时,我得到了同样的错误。但是当我添加第一列标题“regno”并在方法内声明为 NSString 时,清除了错误。

`-(NSArray*)loadData {

     const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
        NSString *querySQL = [NSString stringWithFormat:@"select regno, firstname, middlename, lastname, gender, dob, bloodgroup, mobileno, emailid from personalDetails"];

        NSMutableArray *resultArray = [[NSMutableArray alloc]init];
        const char *query_stmt = [querySQL UTF8String];

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

            if (sqlite3_step(statement) == SQLITE_ROW) {

                ***NSString *regNo = [[NSString alloc] initWithUTF8String:
                                       (const char *) sqlite3_column_text(statement, 0)];
                [resultArray addObject:regNo];***


                NSString *firstname = [[NSString alloc] initWithUTF8String:
                                       (const char *) sqlite3_column_text(statement, 1)];
                [resultArray addObject:firstname];
                NSString *middlename = [[NSString alloc] initWithUTF8String:
                                        (const char *) sqlite3_column_text(statement, 2)];
                [resultArray addObject:middlename];
                NSString *lastname = [[NSString alloc]initWithUTF8String:
                                      (const char *) sqlite3_column_text(statement, 3)];
                [resultArray addObject:lastname];
                NSString *gender = [[NSString alloc]initWithUTF8String:
                                    (const char *) sqlite3_column_text(statement, 4)];
                [resultArray addObject:gender];
                NSString *dob = [[NSString alloc]initWithUTF8String:
                                 (const char *) sqlite3_column_text(statement, 5)];
                [resultArray addObject:dob];
                NSString *bloodgroup = [[NSString alloc]initWithUTF8String:
                                        (const char *) sqlite3_column_text(statement, 6)];
                [resultArray addObject:bloodgroup];
                NSString *mobileno = [[NSString alloc]initWithUTF8String:
                                      (const char *) sqlite3_column_text(statement, 7)];
                [resultArray addObject:mobileno];
                NSString *emailid = [[NSString alloc]initWithUTF8String:
                                     (const char *) sqlite3_column_text(statement, 8)];
                [resultArray addObject:emailid];


                return resultArray;
            }
            else{
                NSLog(@"Not found");
                return nil;
            }
            sqlite3_reset(statement);




        }
    }


    return nil;
}'
于 2016-05-30T10:52:56.270 回答