如何将csv导入sqlite(iphopne的核心数据)
我尝试过使用SQLite管理器,但它将csv导入到新表中,我还需要导入一些日期,
那么如何将数据导入我的sqlite数据库?我有3个具有不同属性的实体,并且在csv中我将所有值都放在一个csv中(所以我可以根据需要对其进行格式化或更改),但是如何改进它?
coredata喜欢的日期格式是什么?
提前感谢!
答案 0 :(得分:3)
我假设您已经设置了CoreData并且运行正常。你真的不想直接使用核心数据的sqlite DB。这是可能的,但也有点凌乱。
Core-Data喜欢NSDate。无论CSV文件在数据列中使用什么,您最好将CSV值转换为NSDate。在您的应用程序中使用NSDate将减少后来的头痛次数。
答案 1 :(得分:0)
我建议,首先使用Core Data在应用程序内创建sqlite数据库,然后使用Core数据创建的数据库导入csv数据。
您可以使用命令行命令导入sqlite文件中的数据。
将sqlite文件添加到项目中。
在persistentStoreCoordinator方法中替换并添加此代码..
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"yourDatabase.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"yourDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
更改方法
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}