Take the 2-minute tour ×

I have an SQL schema that I need to feed to SQLite in my Objective-C command-line app. I’d like the schema to be a part of the binary, so that I can distribute just one file. Unlike a regular Mac or iOS app, the binary has no resource bundle, so the traditional way of storing resources inside the app bundle is out. Is there an elegant way to include the schema in the source? I know I can simply store it as a multiline string in a header, but that sucks.

share|improve this question
Why don't you want to keep it in header? I can't think of any disadvantage of that solution. –  Adam May 28 '12 at 18:06
I’d like to keep the schema in a separate .sql file for easy updates and debugging. –  zoul May 29 '12 at 7:18
add comment

3 Answers


extern NSString * someSchema;


NSString * someSchema = @"CREATE TABLE IF EXISTS blah...."
@"More SQL here"
@"more SQL here";
share|improve this answer
add comment

What about creating an array of sql statements, and then you use an enum to access the statements.

Schema.h file:

static const NSString *sqlStatements[] = {
    @"CREATE TABLE...",
    @"SELECT * FROM ...",
    ... // Lots of other statements
    @"DELETE ..."

typedef enum {
    ... // Matching enums
} SQLCommands;

Used in some other file:

NSString *stmt = sqlStatements[SQLCREATECommand];

The benefit of doing it this way is that the code becomes more maintainable.

share|improve this answer
add comment
up vote 0 down vote accepted

One interesting solution I have come up with is extended attributes. It’s possible to add a Run Script phase to the Xcode build process that does something like:

xattr -w com.company.Schema "`cat SQL/schema.sql`" \

And then in runtime:

const size_t maxSchemaSize = 1000;
char schema[maxSchemaSize] = {0};
getxattr(argv[0], "com.company.Schema", schema, maxSchemaSize, 0, 0);
NSLog(@"%s", schema);

This way I can keep the schema in a separate file without mangling it into a header file. The obvious downside is that the extended attribute might not survive some file operations.

share|improve this answer
add comment

Your Answer


