Saturday, July 24, 2010

SQLite: Easy as 1 2 3 ...4 (Practically Painless)

Yes, SQLite is an excellent SQL solution for the iPhone.

So, how to add support to a project?

I'm assuming that you have an SQLite database already. If you don't, try SQLite Database Browser. It isn't perfect, but it works well enough, and you can use it to create and edit tables. I also assume that you know how to use what SQLite retrieves in your iPhone project. For a more complete guide, you might try this tutorial, but I like to keep my posts focussed. Eventually I'll take the time to learn the pros and cons of using Core Data - I've been led to believe it can do some of the same things that SQLite can do (it can even write to a file in SQLite format) - but until I understand it better I'll stick with what I know: SQL.

Adding the SQLite Framework to your project 

It's as easy as 1 2 3 ...4...

  1. Right-click on Frameworks
  2. Click Add -> Existing Frameworks
  3. In the drop-down, select Dylibs
  4. Select libsqlite3.0.dlib and click Add
Accessing your SQLite database

I'm using fmdb to access the database (it has an MIT license), rather than write my own object for accessing the db. I've copied over the project into a folder I aptly named "FMDB". From there on it should be calm seas! FMDB even comes with a "fmdb.m" file that can be used to see the code in action.

No comments:

Post a Comment