another EXC_BAD_ACCESS
I fixed the error of my last question but my c开发者_JS百科ode raise another EXC_BAD_ACCESS
at
const char *sqlStatement = [NewData UTF8String];
the complete code is
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);
}
Did you try to alloc and init NSString?
NSString *NewData = [[NSString alloc] initWithFormat:@"%@%@%@%@", @"Update animals set description = ",TheNewText , " Where name = ",the_user];
and don't retain it?
I'd bet the string is getting collected by an autorelease run. Look in the docs for UTF8String, they suggest copying the string to ensure it lasting beyond that point.
My real suggestion, after some thought, is to get rid of the assignment to a stepping stone variable.
I'd say use it like this:
if(sqlite3_prepare_v2(database, [NewData UTF8String], -1, &compiledStatement, NULL)== SQLITE_OK) {
sqlite3_reset(compiledStatement);
Note the change in the reset statement. That may be your real problem, your code is trying to execute a string instead of a prepared sqlite statement.
精彩评论