sqlite3 crashes for second insert statement
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:a开发者_如何转开发lertId]];
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);
}
Do you think sqlite3_bind_text() understands what to do with an NSString? I don't.
I'm surprised that
- it works even when i == 0
- 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.
精彩评论