我正在尝试在文本视图部分(名为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);
}
答案 0 :(得分:0)
我不完全确定上面的代码有什么问题,但是这段代码运行得很好:
sqlite3_stmt *stmt=nil;
sqlite3 *cruddb;
//insert
const char *sql = "UPDATE Peaks SET notes=? where ID=?";
//Open db
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cruddatabase = [docsDir stringByAppendingPathComponent:@"highpeaks.db"];
sqlite3_open([cruddatabase UTF8String], &cruddb);
sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [self.viewNotes.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, [self.detailItem ID]);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(cruddb);