DaffodilGirl 2013-01-24 02:46 采纳率: 0%
浏览 4103
已采纳

打开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;
}
  • 写回答

2条回答 默认 最新

  • g989_1314125 2013-01-24 05:13
    关注

    报错可能是由于数据库路径传递为UTF8String,我看见你传递"SoundLib",不能这样直接传递。

     if (sqlite3_open([dbPath UTF8String], &databaseHandle) == SQLITE_OK)
    {
       //dbPath is your full database path you getting above
    }
    

    P.S.dbpath作为const char

    const char *dbpath
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误