2010-05-26 3 views
1

Mon application plante sur l'instruction (sqlite3_step(insertAlert) == SQLITE_DONE) lorsqu'elle est exécutée pour i = 1. Quelqu'un peut-il me dire pourquoi il plante?sqlite3 se bloque pour la deuxième instruction d'insertion

sqlite3_stmt *insertAlert; 

textmeAppDelegate *textme = (textmeAppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSString *insert = @"INSERT INTO alerts (alert_id, email_address, messege_text, when_to_send, how_often_send, recipient_phone_number) VALUES(?, ?, ?, ?, ?, ?)"; 
if(sqlite3_prepare_v2(textme.dbConn, [insert UTF8String], -1, &insertAlert, NULL) == SQLITE_OK) 
{ 
    NSDictionary *tmpDictionary; 
    NSString *alertId; 
    for(int i=0; i<[keys count]; i++) 
    { 
     alertId = [NSString stringWithFormat:@"%@", [keys objectAtIndex:i]]; 
     tmpDictionary = [NSDictionary dictionaryWithDictionary:[dictionary2 objectForKey:alertId]]; 

     sqlite3_bind_text(insertAlert, 1, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"alert_id"]], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(insertAlert, 2, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"email_address"]], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(insertAlert, 3, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"messege_text"]], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(insertAlert, 4, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"when_to_send"]], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(insertAlert, 5, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"how_often_send"]], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(insertAlert, 6, [NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"recipient_phone_number"]], -1, SQLITE_TRANSIENT); 

     if (sqlite3_step(insertAlert) == SQLITE_DONE) { 
      sqlite3_reset(insertAlert); 
      sqlite3_clear_bindings(insertAlert); 
     } 
     else { 
      NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(textme.dbConn)); 
      //[textme showError:@"Device Error" errMsg:sqlite3_errmsg(textme.dbConn)]; 
      return FALSE; 
     } 

    } 
    sqlite3_finalize(insertAlert); 
} 
+0

Vous devriez envisager d'utiliser des données de base à la place. – RunLoop

+0

Et vous devriez envisager d'utiliser un wrapper au lieu d'interagir avec l'API C directement. –

+0

pouvez-vous fournir un bon tutoriel de données de base ou des exemples d'utilisation? –

Répondre

2

Pensez-vous que sqlite3_bind_text() comprend quoi faire avec un NSString? Je ne.

Je suis surpris que

  1. cela fonctionne même quand je == 0
  2. vous ne voyez pas d'avertissement du compilateur sur tous les binds.

Pour vous fixer besoin de remplacer

[NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"alert_id"]] 

avec

[[NSString stringWithFormat:@"%@", [tmpDictionary objectForKey:@"alert_id"]] UTF8String] 

et de même pour toutes les autres lignes sqlite3_bind_text().

+0

Merci j'ai pensé et vous aviez raison que le compilateur donnait un avertissement sur chaque ligne de sqlite3_bind_text() et aucune donnée n'était stockée même pour i == 0 mais elle était en cours d'exécution sans aucun crash (surprise aussi). –

Questions connexes