views:

93

answers:

1

My App is crashing on statement (sqlite3_step(insertAlert) == SQLITE_DONE) when executed for i = 1. Can anybody tell me why it is crashing?

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);
}
+3  A: 

Do you think sqlite3_bind_text() understands what to do with an NSString? I don't.

I'm surprised that

  1. it works even when i == 0
  2. you aren't seeing a compiler warning on all of those binds.

To fix you need to replace

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

with

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

and similarly for all the other sqlite3_bind_text() lines.

JeremyP
Thanks I figured that out and you were right that compiler was giving warning on each line of sqlite3_bind_text() and no data was getting stored even for i==0 but it was executing without any crash(surprised too).
Ayaz Alavi