sqlite on iPhone

Discussion in 'iPhone/iPad Programming' started by realberen, Mar 12, 2008.

  1. macrumors newbie

    Joined:
    Mar 12, 2008
    #1
    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. macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    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. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    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. macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    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. Moderator emeritus

    kainjow

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

    Joined:
    Mar 12, 2008
    #6
    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. macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #7
    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. macrumors newbie

    Joined:
    Mar 12, 2008
    #8
    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?

    -Jeff
     
  9. macrumors 68040

    Joined:
    Apr 22, 2005
    #9
    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. thread starter macrumors newbie

    Joined:
    Mar 12, 2008
    #10
  11. thread starter macrumors newbie

    Joined:
    Mar 12, 2008
    #11
    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. macrumors newbie

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

    https://developer.apple.com/iphone/library/samplecode/SQLiteBooks/index.html

    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. macrumors newbie

    Joined:
    Jun 22, 2008
    #13
    Hello.

    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.
    Thanks.
     
  14. macrumors member

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

    Joined:
    Aug 1, 2008
    #15
  16. macrumors newbie

    Joined:
    Oct 8, 2008
    #16
    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