开发者

SQLite bad access on database path

I've got the following code:

appDel = (Staff_ManagerAppDelegate *)[[UIApplication sharedApplication]delegate];

const char *dbpath = [appDel.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM Staff"]; 
    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while(sqlite3_step(statement) == SQLITE_ROW) {
            NSMutableDictionary *personStuff = [[NSMutableDictionary alloc] init];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)] forKey:@"PersonID"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)] forKey:@"Name"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)] forKey:@"Surname"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)] forKey:@"Email"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)] forKey:@"Phone"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 5)] forKey:@"Birthdate"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 6)] forKey:@"Adress"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 7)] forKey:@"Postal"];
            [personStuff setValue:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 8)] forKey:@"Town"];
            [allStaff addObject:personStuff];
        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(contactDB);
}

My problem is quite simple: At the line const char *dbpath = [appDel.databasePath UTF8String];, I get a BAD ACCESS error.

In the appdel, my database path is created like below:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"staffmanager.sqlite3"];
BOOL success;
NSLog(@"DatabasePath: %@", databasePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];

if(!success)
{
    NSLog(@"Creating path");
    NS开发者_C百科String *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"staffmanager.sqlite3"]];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

What am I doing wrong?


databasePath = [documentsDir stringByAppendingPathComponent:@"staffmanager.sqlite3"];

Here databasePath is an autoreleased string so it can be deallocated any time outside the scope it was created. To persist its value you need to retain it:

databasePath = [[documentsDir stringByAppendingPathComponent:@"staffmanager.sqlite3"] retain];

Or, better, if you have a property for it with retain or copy attribute assign its value via property, not to the iVar directly:

self.databasePath = [documentsDir stringByAppendingPathComponent:@"staffmanager.sqlite3"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜