NEWS:

Note to self & iPhone & mac & os x 28 Nov 2008 03:01 pm

Build and Compile your SQLite Database with Xcode

I’ve been working on setting up a database for an iPhone app. I haven’t worked with sqlite before so it’s yet another new thing to learn. I’ve been going through a few tutorials on the web and was partway through this one when I took the advice of his sidebar at the end of Step 3 to keep my database definition in sql and have it built when the app compiles. He linked to this screenshot by way of a how-to, which unfortunately didn’t work for me.

First off, to find the build rules for your project, right click on Targets in xcode and select Get Info:
main.m - GameDay

I also added a check to see if your db already exists to avoid build errors if it doesn’t. I decided to put it into an external file and execute that from the build rule. Here’s what my build rule looks like:

Target 201CGameDay201D Info
Uploaded with plasq’s Skitch!

Here’s the bash shell script:

#!/bin/bash
cd ${TARGET_BUILD_DIR}
if [ -f ${INPUT_FILE_BASE}.db ];
then
rm ${INPUT_FILE_BASE}.db;
fi
cat ${INPUT_FILE_PATH} | sqlite3 ${INPUT_FILE_BASE}.db

Put it in a file (sqlbuildrule.sh, for example) and place it in your project directory and set it to executable:
chmod +x sqlbuildrule.sh

At the end of the rule, click the + below “with output files” and enter this:
$(TARGET_BUILD_DIR)/$(INPUT_FILE_BASE).db

The script will look for a .sql file in your project directory and build it with the output going to your target build directory.

If you need to edit the shell script or change the “with output files” setting of the build rule, note that the way you use the environment variables is different. The shell uses them as regular bash variables: ${MY_VAR}, where Xcode uses parens: $(MY_VAR).

14 Responses to “Build and Compile your SQLite Database with Xcode”

  1. on 03 Dec 2008 at 11:42 am 1.Jayashree said …

    Once the setup is created for a project, does the user system needs SQLite installed for the application to access the database and process the queries?

  2. on 03 Dec 2008 at 11:48 am 2.Tom said …

    SQLite is installed by default on both Mac OS X and iPhone. You will need the *.db file to be deployed to your iPhone along with the rest of your application bundle, but that is all.

  3. on 30 Dec 2008 at 10:49 am 3.Mike Akers said …

    Thanks for the great tip, but your shell script breaks if there are any spaces in the path to your xcode project. adding some double quotes in the right places fixed the problem. I’m not enough of a shell scripting expert to know if the is The Right Way to fix this problem, but it works :) Here’s my fixed version:

    #!/bin/bash

    cd “${TARGET_BUILD_DIR}”
    if [ -f ${INPUT_FILE_BASE}.db ];
    then
    rm ${INPUT_FILE_BASE}.db;
    fi
    cat “${INPUT_FILE_PATH}” | sqlite3 ${INPUT_FILE_BASE}.db

  4. on 04 Jan 2009 at 11:24 am 4.Bill Wilson said …

    The .sql file has to be added to the “Compile Sources” build phase correct? this seems to be the only way I was able to get this to work.

    Cheers,
    Bill

  5. on 22 Jan 2009 at 9:50 am 5.Nathan said …

    I’m relatively new to mac/iphone development but I’ve got a question.

    I’ve followed the steps and triple checked everything (at least what I think is everything) but when I try to build I keep getting an error “pbxcp: database.db: No such file or directory”. What causes that? I created a .sql file with SQLite Manager and then added it to my project (resources folder). When that failed I moved it to the ‘compile sources’ phase and still no luck.

    thanks for the help.

  6. on 19 Jun 2009 at 2:32 pm 6.Kevin said …

    I have a stupid question I guess. Couldn’t you just import the sqlite3.h file to get the functionality you are looking for here. Then use the sqlite library function calls to create and update the database.

    #import “/usr/include/sqlite3.h”

  7. on 19 Jun 2009 at 3:43 pm 7.Tom said …

    Kevin, you could certainly do that. This is an alternative to adding a db file to your project or having some class create your db if one isn’t found.

    Primarily this is a nice way to version the sql that defines your database. It also ensures you get a clean db every time, plus you could populate your db with default (code table, etc) data as well.

  8. on 04 Jul 2009 at 5:25 am 8.Veyton said …

    thanks for sharing this, it`s awesome.

    keep on .

  9. on 14 Jul 2009 at 1:01 pm 9.Luke Lutman said …

    @Nathan:

    I had the same problem… Make sure that your SQL file IS included in the “Compile Sources” build phase and IS NOT included in the “Copy Bundle Resources” build phase.

  10. on 28 Oct 2009 at 7:01 am 10.Anil said …

    hello,

    I know about how to creating database for i-phone with sqlite.

    !!! thanks,

  11. on 24 Nov 2009 at 1:41 pm 11.Alleinunterhalter said …

    wow, wtf?
    this really works? omg i almost randomly visited this
    page and i found what i’ve been looking for soooo damn long.
    thank you very much. you sir are just great!!!

  12. on 24 Nov 2009 at 1:43 pm 12.Werbetechnik said …

    that is really a cool thing. its easy to handle and
    very useful. next week i will get my iphone and i will
    instantly use this :)
    thanks alot buddy!

  13. on 13 Jan 2010 at 5:08 pm 13.Cuxhaven said …

    I hope it works with the google Nexus too.

  14. on 16 Mar 2010 at 10:33 am 14.Günter Hogrefe said …

    Danke für diese umfangreichen Informationen, mir hilft das sehr weiter…

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply