2009-09-23 48 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