在iPhone5中从SQLite删除数据时应用程序崩溃

时间:2013-06-12 12:44:54

标签: iphone ios objective-c iphone-5

当应用程序“didFinishLoading”数据库清除时,我开发了一个SQLite数据库,并调用Web服务来获取数据并将其插入数据库。它在所有iPhone和iPad设备上运行良好,但在iPhone5上,它在从数据库中删除数据时崩溃。

此处,tableNamesArray表示SQLite中的表。

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    for (int i = 0; i < [tableNamesArray count]; i++)
    {
        NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM %@", [tableNamesArray objectAtIndex:i]];

        NSLog(@"querySQL %@", querySQL);

        // SELECT QuationID,Quation FROM QuationsTable WHERE GroupID="Group0"

        const char *query_stmt = [querySQL UTF8String];
        sqlite3_stmt *compiledStatement;
        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
        }

        if (sqlite3_step(compiledStatement) != SQLITE_DONE )
        {
        }
        else
        {
        }

        sqlite3_finalize(compiledStatement);
    }

    sqlite3_close(contactDB);
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用此功能将数据库复制到缓存目录。

-(void)createDBcopy:(NSString*)fileName
{
    // 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];
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *DBPath=[documentsDirectory stringByAppendingPathComponent:fileName];
    //DBPath = [DBPath stringByAppendingString:@".sqlite"];
    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:DBPath];

    // 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:fileName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];

    [fileManager release];

}

并将其用作:

[yourClassObjectWhereTheMethodIsWritten createDBcopy:@"yourDB.sqlite"];

你应该在Appdelegate的didFinishLaunchWithOption中使用它。