简体   繁体   English

将sqlite文件复制到Documents文件夹后,文件变空

[英]After copying sqlite file into Documents folder, the file getting empty

I have a problem with .db file that after copying into Documents directory getting 0 KB of size, while the original file is 137KB. 我有.db文件的问题,复制到Documents目录后获得0 KB的大小,而原始文件是137KB。 I tried to open my copied file in SQLite Manager. 我试图在SQLite Manager中打开我复制的文件。 It opens, doesn't complaint about file corruption... It just doesn't contain a single table. 它打开,不抱怨文件损坏......它只是不包含单个表。

My code to copy the file: 我复制文件的代码:

- (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]);
    }
}

Checks if file exist, if not copies. 检查文件是否存在,如果不是副本。 The check happens in RootViewController's 检查发生在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];
}

So, that's strange for me. 所以,这对我来说很奇怪。 Any help please? 有什么帮助吗? Thank you in advance. 先感谢您。

After all , I did like this.It worked for me. 毕竟,我确实喜欢这个。它对我有用。

1) click on the .db file. 1)单击.db文件。

2)then find the fileInspector on right side,click the file inspector 2)然后在右侧找到fileInspector,单击文件检查器

3) check the target membership of .db file whether checkbox is checked or not. 3)检查.db文件的目标成员资格是否选中复选框。

4)if not check the checkbox 4)如果没有选中复选框

Hope It will work... 希望它会起作用......

How about you try it with this: 你怎么试试这个:

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");
}

I don't think NSHomeDirectory is a valid way of accessing the documents directory.... 我不认为NSHomeDirectory是访问文档目录的有效方式....

Don't know for sure if this will solve it but it's worth a try. 不知道这是否会解决它,但值得一试。

I don't know if this helps but if you copy a core data '.sqlite' database it also copies it empty, but there are two more files '.sqlite-shm' and '.sqlite-wal'. 我不知道这是否有帮助,但如果你复制核心数据'.sqlite'数据库,它也会将其复制为空,但还有两个文件'.sqlite-shm'和'.sqlite-wal'。 If you copy those two more files with the same name of the destination with their respective extension the database is no longer empty. 如果您使用相应的扩展名复制另外两个具有相同目标名称的文件,则数据库不再为空。

Well, I've solved this problem. 好吧,我已经解决了这个问题。 In my DBAccess class I had method: 在我的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];
    }
}

And it was called in init. 它在init中被调用。 -(void)initializeDataBase is fired at all. - (void)初始化了initializeDataBase。 Now, this piece of code: 现在,这段代码:

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)

I call in every method separately. 我分别打电话给每个方法。 It helped. 它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM