使用iOS应用程序中的现有数据

我很新的iOS开发,绝对不能完全理解Xcode和东西的工作。 我正在创build一个iOS应用程序,并已经使用Sqlite在设备上存储数据。 我已经添加了一个sqlite3数据库的项目,但我注意到,在模拟器中的数据库不存在的第一次运行的应用程序,因此必须在代码中创build。 到现在为止还好。 我应该有一个应用程序随附的date列表。 第一次运行时,应用程序是否可以使用已经有数据的数据库?

 // Creates a writable copy of the bundled default database in the application Documents directory. - (void)createEditableCopyOfDatabaseIfNeeded { // First, test for existence. BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"]; success = [fileManager fileExistsAtPath:writableDBPath]; if (success) return; // The writable database does not exist, so copy the default to the appropriate location. NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if (!success) { NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); } } 

这是复制sqlite数据库,如果它不存在..

希望这可以帮助。

那是因为sqlite文件在你的包里面。

您需要在applicationDidFinishLaunching之后将其移动到库文件夹(或“文档”文件夹)。

签出NSFileManager更多信息…

如何将我现有的SQLite数据库与Core Data一起使用? ! 并有效地导入数据 ! 应该帮你解决。