SQLite数据库 - 添加新列但用户必须卸载&重新安装应用程序

时间:2017-03-16 06:33:39

标签: ios xcode sqlite

我在商店里有一个应用程序。它有一个食谱库。如果我向数据库添加一个新列,例如“营养信息”。在没有用户卸载然后重新安装的情况下添加新列的最佳方法是什么。

- (CEVORecipe *) getRecipe:(NSInteger *)id
{
    CEVORecipe *recipe = [[CEVORecipe alloc] init];

    if ([self open])
    {
        sqlite3_stmt    *statement;

        NSString *sql = [NSString stringWithFormat: @"SELECT rowid,* FROM arecipes WHERE rowid=\"%d\"",(int)id];

        const char *stmt = [sql UTF8String];
        if (sqlite3_prepare_v2(_database,stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {

                NSInteger rowid = sqlite3_column_int(statement, 0);

//                section,name,image,servings,difficulty,preparation_time,instructions,ingredients,photos

                NSInteger section = sqlite3_column_int(statement, 1);
                NSString *name = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                NSString *image = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
                NSInteger servings = sqlite3_column_int(statement, 4);
                NSInteger difficulty = sqlite3_column_int(statement, 5);
                NSInteger preparation_time = sqlite3_column_int(statement, 6);
                NSString *instructions = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 7)];
                NSString *ingredients = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 8)];

                NSString *macros = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 9)];
                NSString *creator = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 10)];
                NSString *profile = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 11)];
             NSString *diet = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 12)];

                NSArray *photoJson = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 13)] componentsSeparatedByString:@";"];



                NSMutableArray *photos = [[NSMutableArray alloc] init];


                for(NSString *photo in photoJson){
                    if(![photo isEqualToString:@""]){
                        [photos addObject:photo];                        
                    }
                }


                recipe = [[CEVORecipe alloc] initWithData:rowid :section :name :image :&servings :&difficulty :&preparation_time :instructions :ingredients :macros :creator :profile :diet :photos];
            }
            sqlite3_finalize(statement);
            [self close];
        }
    }

0 个答案:

没有答案