awkward silence |

Nov/08

28

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).

No tags

14 comments

  • Jayashree · December 3, 2008 at 11:42 am

    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?

  • Admin comment by Tom · December 3, 2008 at 11:48 am

    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.

  • Mike Akers · December 30, 2008 at 10:49 am

    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

  • Bill Wilson · January 4, 2009 at 11:24 am

    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

  • Nathan · January 22, 2009 at 9:50 am

    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.

  • Kevin · June 19, 2009 at 2:32 pm

    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”

  • Tom · June 19, 2009 at 3:43 pm

    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.

  • Veyton · July 4, 2009 at 5:25 am

    thanks for sharing this, it`s awesome.

    keep on .

  • Luke Lutman · July 14, 2009 at 1:01 pm

    @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.

  • Anil · October 28, 2009 at 7:01 am

    hello,

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

    !!! thanks,

  • Alleinunterhalter · November 24, 2009 at 1:41 pm

    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!!!

  • Werbetechnik · November 24, 2009 at 1:43 pm

    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!

  • Cuxhaven · January 13, 2010 at 5:08 pm

    I hope it works with the google Nexus too.

  • Günter Hogrefe · March 16, 2010 at 10:33 am

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

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me