sqlite on iPhone

Discussion in 'iOS Programming' started by realberen, Mar 12, 2008.

  1. realberen macrumors newbie

    Mar 12, 2008
    One much-touted feature of the iPhone is SQLite. Super! I'd love to use it. Noob question, though: how do I use it? What classes do I use to access it? I have installed the iPhone SDK and sifted through the documentation, but I can't find any good documentation or samples on how to access it. The closest I get is "just like any other Mac application". Pointers, anyone? Perhaps even a little sample to write foo into the table bar and read it back again?
  2. admanimal macrumors 68040

    Apr 22, 2005
    SQLite on the iPhone is accessed via the standard SQLite C wrapper, the documentation for which can be found on www.sqlite.org

    This, of course, assumes you have programmed before. If not, you're going to need to do a whole lot more reading before you mess with SQLite.
  3. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    Core Data can use SQLite as a data store as well, which is used by Cocoa.

    There are prolly some Cocoa wrapper frameworks or better yet sample code (Apple says you can't link to third-party frameworks on iPhone OS) to wrap the procedural C api.
  4. admanimal macrumors 68040

    Apr 22, 2005
    I don't believe that Core Data is currently available on the iPhone.

    The SQLite C wrapper is actually pretty simple, I don't think you would get much benefit from wrapping it in additional Objective-C code.
  5. kainjow Moderator emeritus


    Jun 15, 2000
    Check out FMDB, an iPhone-compatible SQLite Cocoa wrapper.
  6. realberen thread starter macrumors newbie

    Mar 12, 2008
    Thanks a bunch, guys. I assumed SQLite would be wrapped in Cocoa on Apple's side, that's why I was confused when I found FMDB and nothing in i.e. Foundation.
  7. Monkaaay macrumors 6502


    Jun 19, 2006
    Richmond, VA
    So, it seems that using the SQLite header files is "easy enough". Is there any information on how to actually use them in the context of an iPhone application? For example, do we need to have some code in our application that checks for an existing database and creates it on the first run if necessary? Or, is there a different approach suggested by Apple? As the original poster suggested, it would be really nice to see a sample iPhone application that does a simple insert and select against a SQLite database.
  8. jalbro macrumors newbie

    Mar 12, 2008
    Also, does the SQLite DB get backed up on the desktop by an iTunes sync?

    Can you access it on the desktop, or is it locked up by iTunes?

  9. admanimal macrumors 68040

    Apr 22, 2005
    At this point there is no way to sync any data from your own apps to a desktop machine. An SQLite database is just a standard file that will be saved in your applications documents directory on the phone.
  10. realberen thread starter macrumors newbie

    Mar 12, 2008
  11. realberen thread starter macrumors newbie

    Mar 12, 2008
    Right, this is actually my main problem. I would like to have a bunch of PDFs on the iPhone, but I have no idea how to sync it except through webservices (I have webservices working, so no problem there), and I would like to sync directly from the desktop. Hope Apple will fix this in june :)
  12. mysticwhiskey macrumors newbie

    Mar 31, 2008
    Apple has some example code for accessing SQLite from an iPhone application here (you'll need to log in with your iPhone developer credentials):


    Like you mentioned, they do actually create the database on first run if it doesn't exist, by making a copy of an embedded database from within the application bundle. To embed the database in the bundle, just add the SQLite database file to the 'Resources' group in XCode.
  13. stark macrumors newbie

    Jun 22, 2008

    I just started with with objective-c/cocoa/iPhone SDK and trying to get the FMDB wrapper working but when I try and compile a fresh project it says:

    error: Cocoa/Cocoa.h not found

    Any help would be greatly appreciated.
  14. rastersize macrumors member

    Apr 9, 2008
    That's because your supposed to import UIKit/UIKit.h ;)
  15. cleverapps macrumors newbie

    Aug 1, 2008
  16. levous macrumors newbie

    Oct 8, 2008
    EntropyDB doesn't work...

    Beware that this library does not install on the iphone device. The author hasn't yet responded to the reported issues on the google code project page. Be sure to check the issues and ensure he has (at least responded) resolved the issues. I built an entire iphone app against it, expecting the problems to be resolved, and now have to build the entire persistence layer after the fact. big disappointment :(

Share This Page