2009-09-23 46 views
0

我準備sqlite的語句,並在準備這個發言我的代碼是breaking.I正在使用的代碼下面一行得到錯誤的sigabart誤差在iPhone上編譯時聲明

if (sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL) != SQLITE_OK) 
{ 

    NSAssert1(0, @"Error: failed to prepare for getConsumptionCount statement with message '%s'.", sqlite3_errmsg(database)); 
} 

這工作完全正常的,但模擬器在iPhone上休息。

我的數據庫db.sql是我轉移到其他資源文件夾的資源文件夾。 我得到SIGABRT錯誤的原因是什麼?就像我知道它是因爲我放在那裏的NSAssert1,但爲什麼我的應用程序沒有準備好聲明。

當我在調試器中看到它說沒有這樣的表,但我很困惑,因爲我的數據庫在那裏,並有表。有什麼我做錯了嗎。如何解決這個問題。

我這樣做以下方式

 databaseName = @"CaloriePacerDatabase.sql"; 
    // Setup some globals 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 

    inFormat = [[NSDateFormatter alloc] init]; 
    [inFormat setDateFormat:@"yyyy-MM-dd"]; 


    databasePath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:databaseName]]; 
    [self checkAndCreateDatabase]; 
    [self openDatabase]; 
    [self compileStatements]; 
    return self; 
} 

- (void) checkAndCreateDatabase 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 
    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 
    // If the database already exists then return without doing anything 
    if(success) return; 
    // If not then proceed to copy the database from the application to the users filesystem 
    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 
    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    [fileManager release]; 

} 

- (void) openDatabase 
{ 
    if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
    { 
     NSAssert1(0, @"Error: failed to open Database '%s'.", sqlite3_errmsg(database)); 
    } 
} 
- (void) compileStatements 
{ 
    //int i = sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL); 
    NSLog(databasePath); 

    if (sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL)!= SQLITE_OK) { 
     NSAssert1(0, @"Error: failed to prepare for getConsumptionCount statement with message '%s'.", sqlite3_errmsg(database)); 
    } 

請讓我知道如果不這樣做的正確方法。

+0

您是否驗證文件副本是否成功從資源?因爲可能存在問題,因此最終會在[openDatabase]上創建相同的新/空數據庫。 – 2009-09-24 06:16:50

回答

0

我也有過類似的問題,因爲在默認情況下打開一個sqlite3的數據庫中創建一個,如果它不存在,如果沒有適當的決定模擬器和你的iPhone之間的路徑可能引起的。

你如何確定數據庫文件(db.sql)的文件路徑?

+0

嘿Nic我更新了問題的方式,我正在做這件事,我正在爲其他應用程序完全正常工作的相同方式。 – rkb 2009-09-23 22:54:11