2012-01-26 7 views
9

Sto avendo alcuni risultati inaspettati con i dati che sto inserendo o sostituendo nel mio database SQLite. Per risolvere il problema, sto cercando di ottenere una stampa completa dello sqlite3_stmt (istruzione) preparato nel seguente codice.iOS/sqlite - Come stampare un sqlite3_stmt preparato in NSLog

Quello che vorrei fare è qualcosa di simile, ma so che non funziona:

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

Esiste un modo per ottenere questo risultato?

Grazie !!

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: non ho trovato una risposta a questa domanda. Ma avevo bisogno di andare avanti così ho finito solo costruendo una stringa con NSLog(); come di seguito per ciascuno dei miei tavoli ho dovuto controllare:

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); 
+0

Cosa problema che stai ottenendo? Qual è l'errore? Puoi postare questo? –

+0

Non ricevo alcun errore. È solo che il comportamento del mio database non funziona come previsto. Vorrei fare dei problemi con un programma manager in sqlite in cui posso provare rapidamente diverse varianti di questa e di altre dichiarazioni. Se potessi stampare ciò che sta accadendo, sarebbe molto veloce per me copiare qualsiasi copia su di esso. Grazie – KevinM

+4

Non penso che tu possa stampare l'istruzione compilata ma puoi stampare la query sql .. se sei nuovo in sqlite in iOS ti consiglio vivamente FMDB .. è un ottimo wrapper per sqlite .. puoi trovare su github https://github.com/ccgus/fmdb – Saurabh

risposta

5

non ho trovato alcun metodo standard per questo, così ho fatto il mio:

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

si chiamano in questo modo:

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

osservazione: l'ho fatto in stessa classe perché io chiamo questo con self ma si può fare in qualsiasi classe

+0

Ho trovato che si interrompe sempre su un 'EXC_BAD_ACCESS' su' sqlite3_mutex_enter (db-> mutex); 'in' sqlite3.c'. –

+0

@ ing0 scusa ma non riuscivo a capire cosa volevi dire con quello. – ademar111190

+0

Ho eseguito il metodo contro il mio stato compilato (sqlite3_stmt *) e si è verificato un errore di runtime. –