I've created an app that uses a SQLite database. The existing code CAN create and use a database in the Library directory.
Now I am creating an App Extension that needs the same database so the database needs to be located in a shared directory.
So I use this to open and create the database:
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"group.com.company.sharedData"];
NSURL *url = [NSURL URLWithString:DATA_FILENAME relativeToURL: groupURL];
NSURL *absURL = [url absoluteURL];
if (sqlite3_open([absURL fileSystemRepresentation], &_theDB) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}
else{
NSLog(@"Opened shared database!");
}
Which works ONCE! I can create open and use the database.
BUT, the next time I open the app, while I can open the DB with the above code everything else yields a SQLite Error 26: (26) SQLITE_NOTADB When attempting to open a file, the SQLITE_NOTADB error indicates that the file being opened does not appear to be an SQLite database file.
NOTE: static NSString *DATA_FILENAME = @"htdata.sqlite3"; I've tested on both the simulator and an iPhone 5S. I have used both 8.0 and 8.1 as targets.