Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS / sqlite - How to print a prepared sqlite3_stmt to NSLog

I'm having some unexpected results with the data i'm inserting or replacing into my sqlite database. To trouble shoot the problem I'm trying to get a full print out of the prepared sqlite3_stmt (statement) in the below code.

What I would like to do is something like this, but I know it doesn't work:

if (sqlite3_step(statement) == SQLITE_DONE) {
            NSLog(@"%@", statement);

Is there anyway to accomplish this?

Thanks!!

sqlite3_stmt *statement;
const char *dbPath = [databasePath UTF8String];

if (true) {

    ListingsObject *temp = (ListingsObject *) DatabaseObject;

    if (sqlite3_open(dbPath, &conyDB) == SQLITE_OK) {

        const char *insertReplaceStmt = "INSERT OR REPLACE INTO listings (id, association_id, name, email, phone, toll_free_phone, fax, website, street, city, state, zipcode, county, bio, featured, hours, lat, lng, updated, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        if (sqlite3_prepare_v2(conyDB, insertReplaceStmt, -1, &statement, NULL) == SQLITE_OK) {
            sqlite3_bind_text(statement, 1, [temp._id UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 2, [temp.associationId UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 3, [temp.name UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 4, [temp.email UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 5, [temp.phone UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 6, [temp.tollFreePhone UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 7, [temp.fax UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 8, [temp.website UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 9, [temp.street UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 10, [temp.city UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 11, [temp.state UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 12, [temp.zipcode UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 13, [temp.county UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 14, [temp.bio UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 15, [temp.featured UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 16, [temp.hours UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 17, [temp.lat UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 18, [temp.lng UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 19, [temp.updated UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 20, [temp.status UTF8String], -1, SQLITE_TRANSIENT);

        }
        if (sqlite3_step(statement) == SQLITE_DONE) {
            NSLog(@"Insert or Replace to Listing Table successful Listing = %@", temp.name);

        }else {
            NSLog(@"Failed to add to Listing table Listing = %@", temp.name);
        }
        sqlite3_finalize(statement);
    }
 sqlite3_close(conyDB);

UPDATE: I haven't found an answer to this question. But I needed to move on so I ended up just constructing a string with NSLog(); as below for each one of my tables I had to check:

NSLog(@"     INSERT OR REPLACE INTO listings (id, association_id, name, email, phone, toll_free_phone, fax, website, street, city, state, zipcode, county, bio, featured, hours, lat, lng, updated, status) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", temp._id, temp.associationId, temp.name, temp.email, temp.phone, temp.tollFreePhone, temp.fax
                  , temp.website, temp.street, temp.city, temp.state, temp.zipcode, temp.county, temp.bio, temp.featured, temp.hours, temp.lat, temp.lng, temp.updated, temp.status);
like image 485
KevinM Avatar asked Jan 26 '12 11:01

KevinM


1 Answers

I did not find any standard method to this, so I made my own:

-(NSMutableString*) sqlite3StmtToString:(sqlite3_stmt*) statement
{
    NSMutableString *s = [NSMutableString new];
    [s appendString:@"{\"statement\":["];
    for (int c = 0; c < sqlite3_column_count(statement); c++){
        [s appendFormat:@"{\"column\":\"%@\",\"value\":\"%@\"}",[NSString stringWithUTF8String:(char*)sqlite3_column_name(statement, c)],[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, c)]];
        if (c < sqlite3_column_count(statement) - 1)
            [s appendString:@","];
    }
    [s appendString:@"]}"];
    return s;
}

You call it like this:

NSLog(@"%@",[self sqlite3StmtToString:statement]);

Observation: I did it in same class because I call this with self but you can do in any class

like image 155
ademar111190 Avatar answered Nov 16 '22 02:11

ademar111190