尝试在SQLite(iOS)中打开数据库时出错

Fel*_*v.- 0 sqlite iphone objective-c ios

我正在使用嵌入式SQLite数据库制作iOS应用程序.所以我在SQLite管理员中创建我的数据库并将其拖到我的Xcode项目中,就像在教程中说的那样.当我尝试打开我的数据库时,我收到此错误:"内存不足".不知道它是否是一个SQLite错误或什么,但我的文件真的很小,以解决内存问题.

这是我的初始化DataBase的代码:

- (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;
Run Code Online (Sandbox Code Playgroud)

}

roh*_*tel 5

错误可能是因为您需要将databasepath传递为UTF8String.我看到你正在传递"SoundLib".你不能直接传球.

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

PS也有dbpath作为const char

const char *dbpath
Run Code Online (Sandbox Code Playgroud)