Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Chapter 13, SQLite Persistence project : errorMsg

We use errorMsg in our NSAssert, but we only defined it as NULL and never used it to get the actual error message. So, it will always be NULL and there is no point to use it in NSAssert.

<...>

char *errorMsg = NULL;


        sqlite3_stmt *stmt;

        if (sqlite3_prepare_v2(database, update, -1, &stmt, nil)

            == SQLITE_OK) {

            sqlite3_bind_int(stmt, 1, i);

            sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL);

        }

        if (sqlite3_step(stmt) != SQLITE_DONE)

            NSAssert(0, @"Error updating table: %s", errorMsg);


<...>

Comments

  • I have the same question here, will anyone give a solution? 
    when I run the app, there is no harm. but then, when I press the home button, the process paused and shows me this: 

    2013-05-20 23:57:50.156 SQLite Persistence[5373:c07] *** Assertion failure in -[LPEViewController applicationWillResignActive:], /Users/Me/Developer/SQLite Persistence/SQLite Persistence/LPEViewController.m:84 2013-05-20 23:57:50.158 SQLite Persistence[5373:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table: (null)' *** First throw call stack: (0x2094012 0x11a1e7e 0x2093e78 0xc37665 0x3c09 0xc624f9 0x20ee0c5 0x2048efa 0xb96bb2 0xe2bb1 0xe2c3d 0xece0c 0xf5e74 0xf6beb 0xe8698 0x1fefdf9 0x1fefad0 0x2009bf5 0x2009962 0x203abb6 0x2039f44 0x2039e1b 0x1fee7e3 0x1fee668 0xe5ffc 0x2b4d 0x2a75) libc++abi.dylib: terminate called throwing an exception (lldb)
  • I have the same issue, too. I am not sure why we encounter this issue. SOS... who can help us.
  • Yeah, you guys are right! Sorry about that. I think that in that section of the applicationDidResignActive: method, since we're not calling a function that returns an error string, we should probably just have something like this inside the for loop:

            // Once again, inline string concatenation to the rescue:
            char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) "
            "VALUES (?, ?);";
            sqlite3_stmt *stmt;
            if (sqlite3_prepare_v2(database, update, -1, &stmt, nil)
                == SQLITE_OK) {
                sqlite3_bind_int(stmt, 1, i);
                sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL);
                if (sqlite3_step(stmt) == SQLITE_DONE)
                    sqlite3_finalize(stmt);
            }

    I haven't actually run that, but my hunch is that it's probably more appropriate.
  • It fixes the problem but I am stuck with the next.

    The app can not read in the data from the database.

    It is failing at sqlite3_prepare_v2, returning 1.

    How can I fix this?



  • ok, looks like it was my bad.


        NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS "

            "(ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";


    FIELD_DATA TEXT was typed as FIELD_DATA_TEXT

Sign In or Register to comment.