[英]After copying sqlite file into Documents folder, the file getting empty
我有.db文件的问题,复制到Documents目录后获得0 KB的大小,而原始文件是137KB。 我试图在SQLite Manager中打开我复制的文件。 它打开,不抱怨文件损坏......它只是不包含单个表。
我复制文件的代码:
- (void) createEditableDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *writableDB = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"yc_ch.db"];
success = [fileManager fileExistsAtPath:writableDB];
if (success)
{
return;
}
NSString *defaultPath = [[NSBundle mainBundle] pathForResource:@"yc_ch" ofType:@"db"];
error = nil;
success = [fileManager copyItemAtPath:defaultPath toPath:writableDB error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file:%@", [error localizedDescription]);
}
}
检查文件是否存在,如果不是副本。 检查发生在RootViewController中
- (void)viewDidLoad
{
self.subCountriesArr=[[NSMutableArray alloc]initWithCapacity:1];
NSMutableArray *subCountriesTemp=[[[NSMutableArray alloc]init]autorelease];
DBAccess *dbAccess=[[DBAccess alloc]init];
[dbAccess createEditableDatabase]; //method to copy file
subCountriesTemp=[dbAccess getSubCountries];
[dbAccess closeDataBase];
[dbAccess release];
}
所以,这对我来说很奇怪。 有什么帮助吗? 先感谢您。
毕竟,我确实喜欢这个。它对我有用。
1)单击.db文件。
2)然后在右侧找到fileInspector,单击文件检查器
3)检查.db文件的目标成员资格是否选中复选框。
4)如果没有选中复选框
希望它会起作用......
你怎么试试这个:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDB = [documentsDirectory stringByAppendingPathComponent:@"yc_ch.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:writableDB] ) {
NSString *databaseFile = [[NSBundle mainBundle] pathForResource:@"yc_ch" ofType:@"db"];
[fileManager copyItemAtPath:databaseFile toPath:writableDB error:nil];
NSLog(@"database copied");
}
我不认为NSHomeDirectory是访问文档目录的有效方式....
不知道这是否会解决它,但值得一试。
我不知道这是否有帮助,但如果你复制核心数据'.sqlite'数据库,它也会将其复制为空,但还有两个文件'.sqlite-shm'和'.sqlite-wal'。 如果您使用相应的扩展名复制另外两个具有相同目标名称的文件,则数据库不再为空。
好吧,我已经解决了这个问题。 在我的DBAccess类中,我有方法:
-(void)initializeDataBase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yc_ch.db"];
// NSString *path=[[NSBundle mainBundle] pathForResource:@"yc_ch" ofType:@"db"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
//
}
else
{
[self dbConnectionError];
}
}
它在init中被调用。 - (void)初始化了initializeDataBase。 现在,这段代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yc_ch.db"];
NSString *path=[[NSBundle mainBundle] pathForResource:@"yc_ch" ofType:@"db"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
我分别打电话给每个方法。 它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.