0

我正在尝试获取用户在文本视图部分(名为插座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);
}
4

1 回答 1

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);
于 2012-07-13T16:39:51.443 回答