首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >objective C中sqlite3_bind_text函数引起的Xcode崩溃

objective C中sqlite3_bind_text函数引起的Xcode崩溃
EN

Stack Overflow用户
提问于 2012-07-14 00:00:35
回答 1查看 1.3K关注 0票数 0

我正在尝试获取用户在文本视图部分(名为viewNotes的outlet)中输入的文本,并在单击提交按钮后将其放入DB中。当我运行这个应用程序时,它工作得很好,直到我点击了提交,然后我得到了一个崩溃(断点)的sqlite3_bind_text函数。我不知道我做错了什么。下面是操作代码:

代码语言:javascript
复制
- (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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-14 00:39:51

我不完全确定上面的代码出了什么问题,但这段代码工作得很好:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11474092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档