ios - 附加数据库时出错不是错误的 sqlite 数据库

标签 ios sql database sqlite

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

    //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);
}

然后我尝试循环所有行。但这是奇怪的部分。里面

最佳答案

您应该将 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 block 中记录错误。

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

关于ios - 附加数据库时出错不是错误的 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413122/

相关文章:

sql - 使用逗号 (,) 从多个表中左连接或选择

mysql - 在不插入的情况下获取唯一标识符

ios - ScrollView 不会在 Swift 中使用 AutoLayout 垂直滚动内容

mysql - 无法使用 Mysql -phpMyAdmin 中的存储过程更新表

ios - 2.10 iPhone 应用程序也必须在 iPad 上运行,无需修改,iPhone 分辨率和 2X iPhone 3GS 分辨率

mysql - Easy SQL 查询 - 对齐外键

mysql - 数据库表设计

mysql - 如何根据mysql中的聚合列添加排名列?

ios - 通过单击第一个按钮 IOS7 更改第二个按钮图像

ios - Interface Builder 生成非 ARC 代码