2013-01-23 50 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

我標記爲正確的前一個,因爲我先讀了它,但是這個工作太人了。謝謝。 –

相關問題