1

我正在为 iOS 开发,我以前的数据库构建得很糟糕。现在已经有 140 个密封但在我的更新中我创建了一个新数据库但我无法删除数据...如何将数据从数据库 A 更改为数据库 B?我在 xCode 5 中使用 sqlite3

4

3 回答 3

1

这是更改数据库表中的列的示例代码:

- (void)alterDB {

    sqlite3_stmt *statement;
    sqlite3_stmt *selectStmt;

    const char *columnExists = "select callCount from lastCall";
    //Alter the table ONLY IF column we want doesn't exist
    if (sqlite3_prepare_v2(database, columnExists, -1, &selectStmt, NULL) != SQLITE_OK)
    {
        NSString *updateSQL = [NSString stringWithFormat:@"ALTER TABLE lastCall ADD COLUMN callCount INTEGER"];
        const char *update_stmt = [updateSQL UTF8String];
        sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE) {
            NSLog(@"DATABASE ALTERED");
        } else {
            NSLog(@"error altering database");
        }
    }

}

我希望这能给你一个想法。

于 2013-11-08T16:45:24.877 回答
0

编写迁移代码,该代码将从数据库 A 中选择数据并将其插入数据库 B。

于 2013-11-08T16:43:12.393 回答
0

在处理不同的模式时,我有一个方法,我总是在打开数据库时调用该方法,该方法检查为此目的预留的表中的模式版本,然后根据需要对模式进行任何更改。

- (void) checkSchema {
    if([[self preferenceForKey:@"SchemaVersion"] isEqualToString: @"1"]) {
        [db sqlExecute:@"create table Documents(documentID int, description text, type text, url text, modifiedDate text, read int, fileName text, needsUpdate int, primary key(documentID,type));"];
        [self setPreference:@"2" ForKey:@"SchemaVersion"];
    }

    // etc. 
}

因此,在开发过程中,我可以添加架构更改,无论有人使用什么版本的应用程序,当他们升级时,他们都会获得最新的架构更改。

正因为如此,以及将我的所有访问器方法更改为我现在使用 SimpleDB ( https://github.com/AaronBratcher/SimpleDB ) 的数据库的工作,我编写了一个键/值数据库。访问数据的完全不同的方式使事情变得更简单。

于 2013-11-08T20:01:07.737 回答