I am trying to take text that the user inputs in a text view section (outlet named viewNotes) and put it into a DB after clicking a submit button. When I run the app it works fine until I hit submit and then I get a crash (breakpoint) on the sqlite3_bind_text function. I can’t figure out what I’m doing wrong. Here is the action code:
- (IBAction)submitNotes:(id)sender {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDir stringByAppendingPathComponent:@"highpeaks.db"];
sqlite3 *database;
sqlite3_stmt *theNewStmt;
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char *sql = "UPDATE Peaks SET notes=? WHERE ID=?";
if(sqlite3_prepare_v2(database, sql, -1, &theNewStmt, NULL) != SQLITE_OK)
NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
}
NSLog(@"%@",self.viewNotes.text);
sqlite3_bind_text(theNewStmt, 1, [self.viewNotes.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(theNewStmt, 2, [self.detailItem ID]);
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(theNewStmt))
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
sqlite3_finalize(theNewStmt);
sqlite3_close(database);
}
I am not entirely sure what was wrong with the above code, but this code works just fine: