开发者社区> 问答> 正文

打开SQlite数据库的时候报错

用嵌入SQLite数据库开发ios应用,我把数据库放到SQlite管理员,然后拖到Xcode工程中。但是当我打开DB的时候,报错 out of memory ,不知道是不是SQlite的bug,因为我的文件很小,应该不会报出内存问题。
初始化数据库的代码:
- (id)initWithPath:(NSString *)path {
if (self = [super init]) {
    BOOL success;
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"SoundLib_DB.sqlite"];

    if ([fileManager fileExistsAtPath:dbPath] == NO) {
        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"SoundLib_DB" ofType:@"sqlite"];
        [fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];
    }

    success = [fileManager fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    sqlite3 *dbConnection;
//Here is when I get the error, at trying to open the DB
    if (sqlite3_open_v2("SoundLib", &dbConnection, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
        NSLog(@"[SQLITE] Unable to open database!");
        NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
        return nil;
    }
    database = dbConnection;
}
return self;
}

展开
收起
a123456678 2016-07-18 13:57:53 2528 0
1 条回答
写回答
取消 提交回答
  • 应该是你忘了 UTF8String

    sqlite3 *database;
    int result = sqlite3_open([dbPath UTF8String], &database);
    if (result != SQLITE_OK) {
        sqlite3_close(database);
        NSLog(@"Error opening databse");
        return;
    }
    2019-07-17 19:57:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载