2013-01-23 49 views
0

正在制作一个带有嵌入式SQLite数据库的iOS应用程序。所以我让我的数据库在一个SQLite管理员,并将其拖到我的Xcode项目,就像在教程中说的。 当我尝试打开我的数据库时,出现此错误:“内存不足”。不知道它是SQLite的错误还是什么,但我的文件很小,以避免内存问题。尝试在SQLite中打开数据库的错误(iOS)

这是我的代码初始化我的数据库:

- (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; 

}

+0

请输入错误 –

+1

您在'sqlite3_open_v2'调用中没有使用数据库的实际文件路径。 – omz

+2

是的,'sqlite3_open_v2(“SoundLib”,&dbConnection,...'不只是鱼腥味,它是错误的 –

回答

5

错误可能是因为你需要传递databasepath为UTF8String。我看到你通过了“SoundLib”。你不能像这样直接传球。

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

P.S.也有dbpathconst char

const char *dbpath 
+0

它现在可以工作了,谢谢你的朋友!! –

+0

任何时候建议:保持阅读官方Apple文档的习惯。只有在教程中才会为你的编码技巧打下一个薄弱的基础。 –

3

更改您的开放进程,使其看起来像这样......这对我的作品;) 我猜你的主要错误是,你忘了UTF8字符串....

sqlite3 *database; 
int result = sqlite3_open([dbPath UTF8String], &database); 
if (result != SQLITE_OK) { 
    sqlite3_close(database); 
    NSLog(@"Error opening databse"); 
    return; 
} 
+0

+1为好的答案。 –

+0

我标记为正确的前一个,因为我先读了它,但是这个工作太人了。谢谢。 –

相关问题