SQLite Step Failed: trying to write a read-only database using a wrapper

I keep getting the error message "SQLite Step Failed: trying to write a database to read" when using this code to copy the database:

-(void)createEditableCopyOfDatabaseIfNeeded { // Testing 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:@"Money.sqlite"]; 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:@"Money.sqlite"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if(!success) { NSAssert1(0,@"Failed to create writable database file with Message : '%@'.", [error localizedDescription]); } } 

I am using the above code in AppDelegate and this is:

 NSString *writableDBPath = [[NSBundle mainBundle] pathForResource:@"Money" ofType:@"sqlite"]; 

In ViewController.m

I am using http://th30z.netsons.org/2008/11/objective-c-sqlite-wrapper/ , what am I doing wrong?

This happens again and again ... It worked fine, but the problem started again.

+4
source share
1 answer

The problem is that you cannot write anything to your kit. To be able to change your database, you need to copy it to the application folder or the cache folder and work with this copy.

+7
source

Source: https://habr.com/ru/post/1309013/


All Articles