SQLite but no copying data file

Discussion in 'iOS Programming' started by boyplunder, May 14, 2009.

  1. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #1
    Hi All,

    I have a couple of apps that I'm working on that use the same core table view code. I'm using a method called 'checkAndCreateDatabase', which I have found many people are using. One of the apps I'm completing needs to ONLY use the data file in the bundle, but I can't for the life of me see what the change is to stop it having to copy the data file to the documents folder.

    The data will be updated regularly, but there should only be one and not in the documents folder. If anyone can point me in the right direction I will be eternally greatful.
     
  2. Menge macrumors 6502a

    Menge

    Joined:
    Dec 22, 2008
    Location:
    Amsterdam
    #2
    Your SQLite file must be outside the bundle folder. If you modify it while it's in the bundle folder, your application's signature will be invalid. The only remaining options are the Documents folder - which is unique to your application and the Library folder - which is also unique to your program. I'd recommend putting it into the documents folder if the database pertains to the working data for your application.
     
  3. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #3
    Thanks for responding.

    The point is that this app will only display the data, which will be searchable, but not editable. I don't want it to copy the database file, just use it.

    If the file is copied to the documents folder, the app registers it as already copied, so any updated datafile will be ignored. I have found that if I change the datafile name each update [for example, if the first update was named database1], it automatically updates the app, but that means there will be multiple datafiles copied for the app. All but the latest will just sit there wasting space. This doesn't seem to me like good housekeeping.

    What does make sense to me is that I just refer to the datafile in the bundle.

    If it MUST be outside the bundle to work, an alternative method, again with a bit of help maybe, would be to copy the file to documents EVERY time the app is opened. Not a big datafile, so that's a possibility.

    Any takers?
     
  4. plinx0r macrumors member

    Joined:
    Jul 11, 2008
    #4
    Take a look at sqlite3.h. More specifically sqlite3_open(), sqlite3_exec(), and sqlite3_close(). To use these you'll need to implement a call back function.

    But, as Menge pointed out, you cannot make changes to this database when it resides in your bundle. That includes any type of updates which you refer to by saying "The data will be updated regularly." Apple code-signs the bundle that you provide to them at the time of submission and changes to that bundle cannot be made.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    The purpose of your checkAndCreateDatabase method is to copy the database file from your app bundle to the Documents folder if there isn't already a copy in the Documents folder. The reason to do this is because you can't modify a file inside the app bundle. If you don't intend to modify the database file then you don't need to use this method. You can just read the database file from the app bundle.

    Regarding updates I add a version to my database file. When opening the database file in the Documents folder my code checks the version number. If it's not the current version number the code then 'does the right thing.'
     
  6. mraheel macrumors regular

    Joined:
    Apr 18, 2009
    #6
    Thats right, I needed to do exactly what you want. Non editable desire needn't be anywhere else but in the app bundle itself.

    The method you created needn't be used at all.

    A good example is the SQLiteBooks.

    method - initialiseDatabase is sufficient.

    Code:
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.lite"];
    if(sqlite3_open([path UTF8String], db) ==SQLITE_OK) { 
    do your stuff
    } 
    I'm still learning. This is the basic code change. I wouldnt really know if there was a better way.

    However, How advisable is it to do it this way? Specially when the DB is meant to be readonly.

    thanks
     
  7. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #7
    One of the first apps I looked into was the SQLiteBooks example, because it was the nearest I could get to what I had in mind. I am a Noob, let's get that straight, and I decided to take my time to work through and learn.

    In saying that, I think the SQLiteBooks example is actually quite complicated. Too complicated for a concept example. I have since learnt other ways and methods that are less complicated, but it didn't instill me with confidence at the time.

    Thank you for the comments and I'll go and try some of the points here.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    One other suggestion is to use FMDB or any other sqlite wrapper rather than the raw sqlite code. FMDB is a little light in the documentation dept but I've found it to be much easier to use than the raw sqlite code.

    http://flycode.googlecode.com/svn/trunk/
     

Share This Page