另一个EXC_BAD_ACCESS

时间:2011-02-21 09:09:08

标签: objective-c xcode ipad ios

我修复了上一个问题的错误,但我的代码引发了另一个EXC_BAD_ACCESS

at

const char *sqlStatement = [NewData UTF8String];

完整的代码是

sqlite3 *database;
// Setup some globals
NSString *databaseName = @"test.sql";

// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString * databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[databasePath retain];
sqlite3_stmt *compiledStatement;

NSString * TheNewText = self.animalDesciption.text; 
[TheNewText retain];

NSString * the_user = AnimalName ;
[ the_user retain];

NSString *NewData = [NSString stringWithFormat:@"%@%@%@%@", @"Update animals set description = ",TheNewText , " Where name =  ",the_user];

[NewData retain] ;

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    const char *sqlStatement = [NewData UTF8String];



    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)== SQLITE_OK) {

        sqlite3_reset(sqlStatement);
    }

2 个答案:

答案 0 :(得分:0)

我敢打赌,字符串是通过自动释放运行收集的。在文档中查找UTF8String,他们建议复制字符串以确保它超出该点。

经过一番思考后,我真正的建议是摆脱踩踏石头变量的任务。

我会说这样用:

if(sqlite3_prepare_v2(database, [NewData UTF8String], -1, &compiledStatement, NULL)== SQLITE_OK) {

        sqlite3_reset(compiledStatement);

注意重置语句中的更改。这可能是您真正的问题,您的代码正在尝试执行字符串而不是准备好的sqlite语句。

答案 1 :(得分:0)

您是否尝试过分配和初始化NSString?

NSString *NewData = [[NSString alloc] initWithFormat:@"%@%@%@%@", @"Update animals set description = ",TheNewText , " Where name =  ",the_user];

并且不保留它?