SQLite对数据库路径的访问不良

时间:2011-06-30 07:58:18

标签: database ios4 sqlite exc-bad-access

我有以下代码:

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);
}

我的问题很简单:在const char *dbpath = [appDel.databasePath UTF8String];行,我收到了一个BAD ACCESS错误。

在appdel中,我的数据库路径创建如下:

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");
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"staffmanager.sqlite3"]];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

此处,databasePath是一个自动释放的字符串,因此可以在创建范围之外的任何时间取消分配。要保持其价值,您需要保留它:

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

或者,更好的是,如果你有一个属性,使用retain或copy属性通过属性赋值,而不是直接分配给iVar:

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