3

当应用程序“didFinishLoading”数据库清除并调用 Web 服务来获取数据并将其插入数据库时​​,我开发了一个 SQLite数据库。它在所有 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);
}

请帮我。

4

1 回答 1

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 中使用它。

于 2013-06-12T13:32:06.480 回答