iphone - 从 iPhone 5 中的 SQLite 删除数据时应用程序崩溃

标签 iphone ios objective-c iphone-5

当应用程序"didFinishLoading" 数据库被清除并调用网络服务来获取数据并将其插入数据库时​​,我开发了一个 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);
}

请帮帮我。

最佳答案

使用此函数将您的数据库复制到缓存目录中。

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

关于iphone - 从 iPhone 5 中的 SQLite 删除数据时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065842/

相关文章:

iphone - MFMailComposeViewController : exit on cancel

ios - 应用程序内购买、Itunes Connect 中的问题

objective-c - 已删除 NSManagedObject 从 TableView 中正确删除,然后在保存时重新出现

ios - 在 Core Data 中使用级联规则删除托管对象的性能注意事项

c# - 使用 Objective Sharpie 绑定(bind) CocoaPods 库

ios - 应用内购买后,viewWillAppear 会多次加载已购买的项目

iphone - 为 UITextView 旋转自定义 inputView 的问题

iphone - 调音台主机音频蓝牙

iphone - 将数据传递回先前分配的 UIViewController

ios - 行间距不起作用