iOS

时间:2015-08-06 05:57:55

标签: ios sqlite

我有一个使用sqlite的iOS应用程序,sqlite数据库文件已预先配置并添加到xcode中。一切都很好,直到我需要在这个sqlite文件中添加一个名为“activities”的新表,所以我添加了数据库文件的表,替换了xcode中的数据库文件,现在在模拟器或真实iOS设备上运行时,它一直抱怨“没有这样的表:活动”。即使我删除了我的设备或模拟器上以前安装的应用程序也无法正常工作。那里发生了什么?顺便说一句,下面是我每次实例化DatabaseManager时处理数据库文件的代码:

- (instancetype)initWithDatabaseFilename:(NSString *)dbFilename {
    self = [super init];
    if (self) {
        // Set the documents directory path to the documentsDirectory property.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        self.documentsDirectory = [paths objectAtIndex:0];

        // Keep the database filename.
        self.databaseFilename = dbFilename;

        // Copy the database file into the documents directory if necessary.
        [self copyDatabaseIntoDocumentsDirectory];
    }
    return self;
}

- (void)copyDatabaseIntoDocumentsDirectory {
    // Check if the database file exists in the documents directory.
    NSString *destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
        NSLog(@"file not exists");
        // The database file does not exist in the documents directory, so copy it from the main bundle now.
        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseFilename];
        NSError *error;
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:&error];

        // Check if any error occurred during copying and display it.
        if (error != nil) {
            NSLog(@"%@", [error localizedDescription]);
        }
    } else {
        NSLog(@"file is already there");
    }
}

1 个答案:

答案 0 :(得分:0)

通过copyDatabaseIntoDocumentsDirectory方法中生成的终端路径从文档目录中删除数据库文件,拖放项目中的数据库文件。确保通过终端创建表格。