Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I grab the error code if sqlite3_prepare_v2() returns one?

Largely following Ray Wenderlich's SQLite tutorial, I'm writing a fairly straightforward app that displays information pulled down from a server. The basic structure of my SQLite queries (to the local database, not the server) is as follows:

{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    NSString *query = @"SELECT _id, type FROM table ORDER BY type"; // As appropriate.
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            // Process the returned values...
            int rowNumber = sqlite3_column_int(statement, 0);

            // Initialize the object.
            // Push the object onto the array.
        }
        sqlite3_finalize(statement);
    } else {
        // Log the error.
    }
    return list;
}

My question is, what's the best way to check for an error if the if statement returns false? The simplest solution to me seems to store the return value of sqlite3_prepare_v2() as an int and check that against SQLITE_OK. What about calling sqlite_errmsg()?

I did look into sqlite_exec() but I haven't had any luck in getting forays to compile and I don't feel confident enough in my understanding of callbacks in C to properly maintain code that uses them. It passes an argument for the error though, which is what got me looking into it in the first place.

like image 976
ele Avatar asked Feb 14 '13 16:02

ele


1 Answers

If you want to get an error message, you must indeed call sqlite3_errmsg.

sqlite3_exec would give you the same string.

like image 119
CL. Avatar answered Oct 21 '22 08:10

CL.