【问题标题】:Update/Replace two different database table column更新/替换两个不同的数据库表列
【发布时间】:2015-03-04 10:04:24
【问题描述】:

我有两个不同的数据库,Databse1 和 Database2,在这两个数据库中都包含同名表“tblSubUnit”,该表有一个列名为“Favorite”,现在我想替换Database1 表列与 Database2 表列(收藏夹)。所以请帮助我如何替换/更新这个。

  1. 我已经为此创建了一个函数来附加这两个数据库,但是在我无法更新/替换列之后,请帮助我提前感谢。

代码:

- (void)UpdateFavoriteData:(NSString*)query{

    NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];

    if(path==nil){
        path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
    }
    NSString *l_DatabasePathFromApp = [path stringByAppendingPathComponent:self.m_DatabaseName];

    sqlite3 *bookLoansDB;
    if (sqlite3_open([self.m_DatabasePath UTF8String], &bookLoansDB) == SQLITE_OK) {
        NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS FavoriteData", [l_DatabasePathFromApp UTF8String]];
        char *errorMessage;
        if (sqlite3_exec(bookLoansDB, [attachSQL UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK) {
            sqlite3_stmt *selectStmt;

            NSString *selectSQL = @"UPDATE [FavoriteData.SubUnit], [main.SubUnit] SET [FavoriteData.SubUnit].[Faverite] = [main.SubUnit].[Faverite]";
            if (sqlite3_prepare_v2(bookLoansDB, [selectSQL UTF8String] , -1, &selectStmt, nil) == SQLITE_OK) {
                if(sqlite3_step(selectStmt) == SQLITE_DONE) {
                }
            }
            else {
                NSLog(@"Error while Update statement: '%s'", sqlite3_errmsg(bookLoansDB));
            }
        }
        else {
            NSLog(@"Error while attaching databases: '%s'", errorMessage);
        }
    }
    else {
        sqlite3_close(bookLoansDB);
    }

}

我尝试了这么多更新查询,但每次“更新语句时出错”都得到相同的结果,所以请帮助我如何用另一个数据库表列数据更新一个数据库表列。

【问题讨论】:

  • 不,不一样,请仔细阅读我的问题。我有两个不同的数据库。
  • 但是你的问题是UPDATE语句的语法。
  • 是的,如果您知道这一点,或者您有解决方案,请发布答案,我将不胜感激,这对我没有帮助,我已经尝试过了。
  • 您的语法错误。我链接到的答案中的语法是正确的。当然,您必须使用正确的表/列名称。

标签: ios objective-c iphone sqlite


【解决方案1】:

我得到了解决方案,我终于得到了正确的查询。请参阅下面的答案。

- (void)UpdateFavoriteData:(NSString*)query{

    NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];

    if(path==nil){
        path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
    }
    NSString *l_DatabasePathFromApp = [path stringByAppendingPathComponent:self.m_DatabaseName];


    sqlite3 *bookLoansDB;
    if (sqlite3_open([self.m_DatabasePath UTF8String], &bookLoansDB) == SQLITE_OK) {
        NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS FFavorite", [l_DatabasePathFromApp UTF8String]];
        char *errorMessage;
        if (sqlite3_exec(bookLoansDB, [attachSQL UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK) {
            sqlite3_stmt *selectStmt;

            NSString *selectSQL = @"update FFavorite.SubUnit set Faverite = (SELECT main.SubUnit.Faverite FROM main.SubUnit WHERE FFavorite.SubUnit.unit_id = main.SubUnit.unit_id)";
            if (sqlite3_prepare_v2(bookLoansDB, [selectSQL UTF8String] , -1, &selectStmt, nil) == SQLITE_OK) {
                if(sqlite3_step(selectStmt) == SQLITE_DONE) {
                }
            }
            else {
                NSLog(@"Error while creating select statement: '%s'", sqlite3_errmsg(bookLoansDB));
            }
        }
        else {
            NSLog(@"Error while attaching databases: '%s'", errorMessage);
        }
    }
    else {
        sqlite3_close(bookLoansDB);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 2021-09-04
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2019-10-31
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多