Building SQLite into your IOS applications


When I first started with IOS development, I used SQLite as the main database engine for my first iPhone application, AutoXLogger.  I recently started looking at the effort involved to revive this project and update it for IOS 7.0 and beyond. The version of SQLite that ships with Xcode is quite old and it lacks many of the newer features found in new builds. To get around this problem I created a static library that is built using the latest SQLite build.  It is really not too hard of a project once you get the proper build settings configured.  SQLite is very portable and it is not hard to build it on  IOS.

Building the library

To build a static library that you can use in your IOS applications is an easy task.

Create the library

step1 create IOS library project

Using Xcode, create a new static library for IOS.

Step2 name the project

Give the new project a name.

manipulate source code

Step3 delete default xcode files

Delete the default Xcode generated files. We will not be using these files.

Step4 Add SQLite source files

Add the SQLite source files.

configure build

SQLite offers a lot of build options that let you configure the performance and default settings built into the source code.  Some are helpful for tuning the database performance for the limitations of the mobile platform, but most of the time you won’t need to touch them.  You can view a full list of those options here: Compilation Options For SQLite.  Below are a  list of  options that I think might be useful on the mobile platforms

configure default values

  • SQLITE_DEFAULT_AUTOVACUUM=<0 or 1 or 2> Determine how the file size management and how free space is managed in the database file.  default is off(0).  See the documentation on the auto_vacuum flag for details on this feature.
  • SQLITE_DEFAULT_CACHE_SIZE=<pages>  This sets the default size of the page-cache for each attached database, in pages. This can be overridden by the PRAGMA cache_size command. The default value is 2000.

Operational control settings

  • SQLITE_SECURE_DELETE  This causes deleted content to be written overwritten with zeros.
  • SQLITE_THREADSAFE=<0 or 1 or 2>  Configures the threading model used bySQLite.
    • When compiled with SQLITE_THREADSAFE=0 all mutexing code is omitted and it is unsafe to use SQLite in a multithreaded program.
    • When compiled with SQLITE_THREADSAFE=1 SQLite can be used in a multithreaded environment. This is the default.
    • When compiled with SQLITE_THREADSAFE=2, SQLite can be used in a multithreaded program so long as no two threads attempt to use the same database connection (or any prepared statements derived from that database connection) at the same time.

    See the threading mode documentation for additional information on aspects of using SQLite in a multithreaded environment.

Enabling features

  • SQLITE_ENABLE_FTS4  When enabled, the string searching capabilities of the database engine are significantly enhanced.  This produces a performance gain when executing a query against the database, so enabling this feature is recommended.
Step5 enable SQLITE_ENABLE_FTS4

Enable the fast text searching by adding the SQLITE_ENABLE_FTS4 preprocessor flag

  • SQLITE_ENABLE_MEMORY_MANAGEMENT  This option allows SQLite to release unused memory upon request.

Analysis and Debugging Options

  • SQLITE_DEBUG  Turns on the debug asserts() used within SQLite.
  • SQLITE_MEMDEBUG  Turns on a debug memory allocator, similar to debug memory routines used in debug mode builds.

test the library

At this point you should have a static library building for your copy of SQLite ready to include in your Xcode project.   Up next we will write a test app to drive your new SQLite library.  In the mean time, if you want to try and build your own copy of this library, you can either download the SQLite source code yourself and follow these steps, or download the complete library project from my GitHub account.

About these ads

4 thoughts on “Building SQLite into your IOS applications

  1. Pingback: Using Sqlite3 in your IOS application part 1 | conedogers

  2. Pingback: Using SQLite in your IOS application part II | conedogers

  3. Pingback: Using SQLite in your IOS applications part III | conedogers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s