我正在尝试获取用户在文本视图部分(名为插座viewNotes
)中输入的文本,并在单击提交按钮后将其放入数据库中。当我运行应用程序时,它工作正常,直到我点击提交,然后我在sqlite3_bind_text
函数上遇到崩溃(断点)。我无法弄清楚我做错了什么。这是操作代码:
- (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);
}