Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database crash issue at the time of retrieving data

Tags:

sqlite

iphone

enter image description here This is the Database field values.

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)
{

}

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

Error at the time of 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

Please guide me where is the actual problem lies.

like image 743
Jignesh Fadadu Avatar asked Jan 31 '13 05:01

Jignesh Fadadu


People also ask

What causes database to crash?

A great number of database crashes are caused by file permission issues, corrupted data, and index files. There are several reasons for this: 1.) Other processes are modifying a data or index that is written by the database without accurate locking.

What happens if database crashes?

If the database disk crashes, you can restore from a backup plus the logs that cover from the point of the backup. If the log disk fails, you might quickly start logging to a different location/disk and generate a fresh backup (your database might be vulnerable for a time while you fix this).

What is database crash recovery?

Crash recovery is the process by which the database is moved back to a consistent and usable state. This is done by rolling back incomplete transactions and completing committed transactions that were still in memory when the crash occurred (Figure 1). Figure 1. Rolling back units of work (crash recovery)


2 Answers

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, ...)];

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];
like image 160
lnafziger Avatar answered Sep 24 '22 14:09

lnafziger


you have to check first data is NULL or not befor Bind it like bellow:-

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

Impliment it in your method bellow:

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

might be its helps you :)

like image 39
Nitin Gohel Avatar answered Sep 24 '22 14:09

Nitin Gohel