For my
Eponyms iPhone App I wanted to bless SQLite with the ability to handle special chars - especially German Umlaute - like they should be handled, which e.g. is treating
ä like
a when sorting. Currently these Umlauts appear after Z, which is not what we want.
SQLite offers the ability to compile with complete Unicode support, provided by the
ICU Project, but that will bloat the library and is not too easy to cross compile to the iPhone platform. This is also what
Ioannis Epaminonda thought and he took action and provides a solution including "only" the desired features of the Unicode package, resulting in a much smaller footprint.
Here's how to get this to work for your SQLite-using iPhone App, implemented as a static library:
Create your own SQLite static library
- Create a new Project in Xcode, use the iPhone » Library » Cocoa Touch Static Library Template. Pick a good name, the library will be called libYourName.a in the end
- Download the SQLite Amalgamation source - that is all of SQLite in one (well, two with the additional header) file. Add the Header and the main file to the currently empty project
- Download Ioannis source code files, and add the two files (.h and .c) to your project. Your project should now look like this:
- Add SQLITE_CORE and SQLITE_ENABLE_UNICODE preprocessor definitions:
Call up the project's information window, select the "Build" tab and click the gogwheel icon, then select "Add User-Defined Setting". Add the two statements under the keyword GCC_PREPROCESSOR_DEFINITIONS.
- By default the library template will enable Objective C code, but we don't need this. Remove the -ObjC flag from the Project's Other Linker Flags in the project's information window and remove the Foundation Framework from the frameworks:
- At the very last, the unicode c file still contains the line #include <sqlite3.h>. This will include the system-wide header file, but we want it to include the header file we've just downloaded. So we add "." to our header search paths (don't check the recursive checkbox when asked):
At this point you can hit the Build Button and Xcode will already generate the libX.a static library file which you could already use. But we're taking advantage of Xcode's ability to
cross reference projects, this way you'll only have to build your original project and the library will be built using your current build settings; for each build you'll have the latest version of your static library for your current settings, which is quite convenient.
Add the library to your iPhone project
- Open your iPhone project in Xcode, the SQLite project still open
- Drag and drop the SQLite project into the Groups & Files Section of your iPhone project:
In the sheet that pops out, do NOT check the copy items into...; we want a reference to that project, not a copy:
- Now we tell Xcode to use our own SQLite library, not the general one we've used so far. Remove the SQLite library from Frameworks in case it's still there. No need to add our own there, it's already known to Xcode since we added a reference to our project. But we need to tell Xcode that it must copy this library to the application bundle, otherwise your users will not get that library and the app will of course not run. Our library appears right where we inserted the reference to the library project, you can simply drag it from there down to the Copy Bundle Resources target group. In case Xcode did not do this automatically, also make sure that it appears in the Link Binary With Libraries group.
- Xcode will use the build settings of the main project also for linked projects, so make sure you add the GCC_PREPROCESSOR_DEFINITIONS also to the settings of your iPhone project (See step 4 of Create your own SQLite static library)
- We need to load these Unicode extensions on App launch and unload them before App quit; I've included the corresponding functions in the function that initially opens a database connection and the function that closes it again:
- On launch: sqlite3_unicode_load();
- Before quit: sqlite3_unicode_free();
- That should be all, compile and Run!
I hope this helps, in case I missed something leave a comment. In case you want to do cross referencing more often, there's a more dedicated way in doing this by adding constants to Xcode and define a shared build directory. See
Clint Harris Blog for how to do this.
Pascal: It should not have a problem with parentheses, any valid CSV should work. Is the file Ending ".csv"? Can you send me the output of the following Terminal command?
qlmanage -d1 -t file.csv