Update existing SQLite DB within XCode Project

Discussion in 'iOS Programming' started by ews2001, Sep 18, 2008.

  1. macrumors newbie

    Sep 18, 2008
    After I created my sqlite database in the terminal window, I then added the file as a resource to my project and I'm able to access the data.

    However, I've tried over 20 different ways to edit the data in the database or start over with a new database (using the terminal, cleaned the project, deleted the resources then added them back in) and NOTHING has worked.

    What am I doing wrong?
  2. Moderator emeritus


    Jun 15, 2000
    You're trying to replace the database from within Xcode?

    What you described should have worked. Sometimes Xcode doesn't always copy a resource over, but cleaning should fix it.
  3. thread starter macrumors newbie

    Sep 18, 2008
    Yes, I'm trying to update the SQLite database with new data, I want to insert some new rows and change some column data types.

    Some things I've attempted:
    Deleting all instances of my_old_database.sqlite files within my project folder, removing it as a resource. Then I go into terminal, create a new project, ie: my_new_database.sqlite and add it as a resource.
    I clean the project
    Build the project and in my iPhone simulator the old data is still there...

    What am I doing wrong here?
  4. macrumors member

    Jul 9, 2008
    if you're following the sample code on using sqlite, it has you copying the db from the resources to the user's directory and using that copy. There is a safegaurd to prevent copying over a user's data. So if you replace the db in the project, your code will still see the existing db in the user directory and not copy your new db.

    Hope that helps.
  5. macrumors 6502

    Sep 17, 2003
    I usually manually delete my old Application from ~/Library/Application Support/iPhone Simulator/User/Applications (path from memory as I'm at work, may be slightly wrong)

    That forces it to copy the entire new app to the Simulator.
  6. thread starter macrumors newbie

    Sep 18, 2008
    Thanks for your help, everyone.

    The only way I was able to get it to work was by manually removing the old database from my Documents/Project_Name folder, then I scripted a new sqlite DB (naming it different) and saved it within my Project_Name folder, added it as a resource, then renamed all references of my new DB within my application files (appDelegate and others).

    I guess this is the only way to go about it...just frustrating to have to rename all references to the new DB within my application itself each time I want to update my sqlite DB file.

    There has to be an easier way...
  7. macrumors 68030


    Sep 2, 2008
    I have a similar situation. I just have some code that deletes the existing db in the Documents folder and then writes a new one there. This code will obviously only run in my debug builds. I copy in a new db every time the debug app launches.

    You could also write code that checks the mod dates on the two db files and deletes the one in the Documents dir if its date is older than the one in the app.

    Another thing I do is write a version number into a table in the db. I have a currentVersion constant in my app. When the app is going to load a db it checks the version number in the db and if it doesn't match the current one in the app it deletes the db file and rebuilds it. I bump the currentVersion every time I make a change to the db schema, which can happen multiple times per day during development.

Share This Page