1

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.

Ken
  • 21
  • 2
  • There's nothing wrong with that code, nor any fundamental reason that an app extension would corrupt/destroy a SQLite file. It might help to inspect the file directly (e.g. by finding it in the simulator directory on OS X) to see what it looks like. At least then you'd know what's actually wrong with it. – Tom Harrington Feb 18 '15 at 17:42
  • Thank you, that is what I've been doing this morning. I'm using Liya to look at the DB. Do you know of a better tool? The file is only 475 bytes even after I add 20+ records. PS: I like your blog, I've been reading it hoping there was something I had missed. – Ken Feb 18 '15 at 17:45
  • Is that 475 byte file a valid SQLite file? I usually just use sqlite3 from the command line, but then I don't open SQLite files very often. If it were my code I'd want to carefully track that database reference's lifetime to see if you're corrupting it somewhere (maybe not closing it when you're done?). – Tom Harrington Feb 18 '15 at 17:57
  • When I change the paths back to Library it all just works. DB grows to 37k with 24 records. – Ken Feb 18 '15 at 18:04
  • I've thought about the corruption issue and I'm trying what you suggested. Would you close and open the DB when the app goes active and inactive? I've never done it that way but then I've never had a problem like this. – Ken Feb 18 '15 at 18:12
  • Thanks for the help and the blogs. I knew it had to be a bonehead mistake that just wasn't seeing. I was overwriting the file with my settings archive! D'oh! – Ken Feb 18 '15 at 18:48

0 Answers0