附加数据库时错误不是sqlite数据库错误

时间:2014-06-25 15:43:42

标签: ios sql database sqlite

所以这是我的目标:我需要将一个同步数据库附加到我的主数据库,并将任何字段更新或替换到我的主数据库中。所以我首先附加我的数据库。然后我尝试通过所有表格。这是古怪的部分:在我的主查询字符串内部,当我说: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);
}

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

1 个答案:

答案 0 :(得分: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) {

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

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