【问题标题】:Error not an error sqlite database while attaching database附加数据库时出错不是错误的sqlite数据库
【发布时间】:2014-08-16 06:42:10
【问题描述】:

所以这是我的目标:我需要将一个同步数据库附加到我的主数据库,并将任何字段更新或替换到我的主数据库中。所以我首先附加我的数据库。然后我尝试浏览所有表格。这是古怪的部分:在我的主查询字符串中,当我说:SELECT name FROM sqlite_master 时,if 语句没有执行并显示“错误:不是错误”。现在,当我告诉主查询 SELECT name FROM sync_db.sqlite_master 时,if 语句就会执行。但是,我收到一条错误消息,说没有这样的表:sync_db.sqlite_master 存在。有人可以引导我完成正确的协议吗?提前致谢。

    //Atataching the sync db to the master db

    NSString *attachSQL = [NSString stringWithFormat:@"ATTACH DATABASE \'%@\' AS sync_db", dbPathSync];

    NSLog(@"Here's the arratch string: %@", attachSQL);

    //
    if ((errorNum = sqlite3_exec(mainOpenHandle, [attachSQL UTF8String], NULL, NULL, &errorMessage)) == SQLITE_OK) {

        NSString *masterQuery = [NSString stringWithFormat:@"SELECT name FROM sync_db.sqlite_master WHERE type='table';"];
        const char *masterStmt = [masterQuery UTF8String];
        sqlite3_stmt *statement;

        //If statement does not execute and prints error saying "not an error" when
        //place SELECT from "sqlite_master" inside master query. 
       if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)) {
            while (sqlite3_step(statement) == SQLITE_ROW) {

                NSString * currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

                NSLog(@"Here's the current table: %@",currentTable);

                //This is where the magic happens. If there are any keys matching the database, it will update them. If there are no current keys in the database, the query will insert them.
                if ([currentTable isEqualToString:@"USER_DATA"] == NO && [currentTable isEqualToString:@"USER_ACTIVITY"]== NO && [currentTable isEqualToString:@"USER_ITINERARY"] == NO) {
                    NSString *tblUpdate = [NSString stringWithFormat:@"INSERT or REPLACE INTO main.%@ SELECT * FROM sync_db.%@;",currentTable, currentTable];
                    const char *updateStmt = [tblUpdate UTF8String];
                    if ((errorNum = sqlite3_exec(mainOpenHandle, updateStmt, NULL, NULL, &errorMessage))!= SQLITE_OK) {

                        if (errorNum == 1) {
                            //A database reset is needded

                            self->isResetDataBase = YES;
                        }
                        dbErr = YES;
                    }
                }
            }
             NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
        }
        NSLog(@"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(mainOpenHandle));

        NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
        sqlite3_finalize(statement);
        //Detaching the database from the mainDB
        NSString *detachSQL = [NSString stringWithFormat:@"DETACH DATABASE sync_db"]; // reference sync db
        if ((errorNum = sqlite3_exec(mainOpenHandle, [detachSQL UTF8String], NULL, NULL, &errorMessage))!= SQLITE_OK) {

            NSLog(@"Detatched syncDb Failed. ErrorMessage = %s ", errorMessage);


        }
    }


}



NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));

//Closing the database when finished.
if (mainOpenHandle != nil) {
    sqlite3_close(self.mainOpenHandle);
}

if (syncOpenHandle != nil) {
    sqlite3_close(self.syncOpenHandle);
    NSError *err;
    int success = [fileManager fileExistsAtPath:dbPathSync];
    if (success) {
        [[NSFileManager defaultManager]removeItemAtPath:dbPathSync error: &error];
    }

}

if (userOpenHandle != nil) {
    sqlite3_close(self.userOpenHandle);
}

然后我尝试遍历所有行。但这是古怪的部分。内部

【问题讨论】:

    标签: ios sql database sqlite


    【解决方案1】:

    您应该将sqlite3_prepare_v2 的结果与SQLITE_OK 进行比较。

    当你这样做时:

    if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)) {
    

    那么if 语句只有在出现错误时才会成功。你想要:

    if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL) == SQLITE_OK) {
    

    您还应该更新您的代码以仅在 if 语句的 else 块中记录错误。

    if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL) == SQLITE_OK) {
        // process query
    } else {
        // log error here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多