Database crash during data retrieval

enter image description here These are the values ​​of the database fields.

Below is the code I use to get 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 application block, the code does not occur inside

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

Here it gets statement = 0x00000000 , and I think for this reason only it is not included in the if block.

Error during crash

 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 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) 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 tell me where the real problem is.

+4
source share
5 answers

This is caused by an attempt to create an NSString object with a NULL string. It is on one of the following lines:

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

So, before you create an NSString with the results of the sql statement, you need to check for NULL as follows:

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

you need to check that the first data is NULL or not in order to bind it as shown below: -

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

Add this to your method below:

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

can help you :)

+2
source

Write sqlite3_finalize(statement); inside the if condition:

  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); } 
+1
source

I went through the same error.
Instead of checking for NULL or null values, we can modify the SQL query to return NOT NULL / BLANK values.

In the query below, the restaurant name is checked for empty / zero @ values ​​at the database level.

NOTE. Sorry for the syntax error, if there is one .. We examined only 1 column.

Example:

 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)]; } } 
+1
source

in my case, I get the same error when I missed the first column of "regno" in the db table. But when I add the first header'regno column and declared as NSString inside the method, the error is cleared.

 `-(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; }' 
+1
source

All Articles