2012-01-26 15 views
9

Mam pewne nieoczekiwane wyniki z danymi, które wstawiam lub zamieniam w mojej bazie danych SQLite. Aby rozwiązać problem, próbuję uzyskać pełny wydruk przygotowanego sqlite3_stmt (instrukcja) w poniższym kodzie.iOS/sqlite - Jak wydrukować przygotowany sqlite3_stmt do NSLog

Co chciałbym zrobić coś takiego, ale wiem, że to nie działa:

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

Czy mimo to do osiągnięcia tego celu?

Dzięki!

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: nie znalazłem odpowiedzi na to pytanie. Ale musiałem iść dalej, więc skończyłem właśnie konstruując ciąg znaków z NSLog(); jak poniżej dla każdego z moich tabelach musiałem sprawdzić:

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

Co problem, który otrzymujesz? Jaki jest błąd? Czy możesz to opublikować? –

+0

Nie dostaję żadnego błędu. Po prostu zachowanie mojej bazy danych nie działa zgodnie z oczekiwaniami. Chciałbym rozwiązywać problemy w programie do zarządzania sqlite, gdzie mogę szybko wypróbować różne wariacje tego i innych stwierdzeń. Gdybym mógł wydrukować, co się dzieje, bardzo szybko skopiowałbym go. Dzięki – KevinM

+4

Nie sądzę, możesz wydrukować skompilowane oświadczenie, ale można wydrukować kwerendę sql .. jeśli jesteś nowy w sqlite w iOS gorąco polecam FMDB .. jego bardzo dobre opakowanie dla sqlite .. można znaleźć to na github https://github.com/ccgus/fmdb – Saurabh

Odpowiedz

5

Nie mogę znaleźć żadnej standardowej metody na to, więc zrobiłem mój własny:

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

to nazwać tak:

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

Obserwacja: zrobiłem to w tej samej klasie, bo nazywają to z self ale można to zrobić w każdej klasie

+0

Znalazłem to zawsze łamie na 'EXC_BAD_ACCESS' na' sqlite3_mutex_enter (db-> mutex); 'w' sqlite3.c'. –

+0

@ ing0 przepraszam, ale nie mogłem zrozumieć, co przez to rozumiesz. – ademar111190

+0

Uruchomiłem twoją metodę przeciwko mojej skompilowanej instrukcji (sqlite3_stmt *) i wystąpił błąd środowiska wykonawczego. –