sqlite ios : attempt to write a readonly database

前端 未结 5 1603
天涯浪人
天涯浪人 2021-01-15 05:52

I use a sqlite database for a project. I can do queries like SELECT but impossible to do INSERTs! On the simulator the INSERT works properly. As soon as I compile on my iPod

5条回答
  •  情歌与酒
    2021-01-15 06:30

    -(void)createEditablecopyofDatabaseifneeded
    {
        BOOL success;
        NSError *error;
        NSFileManager *filemanagr = [NSFileManager defaultManager];
    
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *dbPath = [path objectAtIndex:0];
        NSString *finalDBPath = [dbPath stringByAppendingPathComponent:mDataBaseName];
        self.mDataBasePath=finalDBPath;
        success = [filemanagr fileExistsAtPath:finalDBPath];
        if (success) {
    
        }
        else
        {
            NSString *writableDBPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:mDataBaseName];
            success = [filemanagr copyItemAtPath:writableDBPath toPath:finalDBPath error:&error];
            self.mDataBasePath=writableDBPath;
        }
        if (!success) {
    
        }
    }
    

    Check this before you create the DB in the document directory Hope this will help you

提交回复
热议问题