PDA

View Full Version : sqlite on iPhone




realberen
Mar 12, 2008, 06:07 PM
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?



admanimal
Mar 12, 2008, 06:24 PM
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?

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.

Sayer
Mar 12, 2008, 06:32 PM
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.

admanimal
Mar 12, 2008, 06:33 PM
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.

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.

kainjow
Mar 13, 2008, 12:27 AM
Check out FMDB (http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html), an iPhone-compatible SQLite Cocoa wrapper.

realberen
Mar 13, 2008, 02:18 AM
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.

Monkaaay
Mar 13, 2008, 07:50 AM
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.

jalbro
Mar 13, 2008, 02:49 PM
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

admanimal
Mar 13, 2008, 07:28 PM
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

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.

realberen
Mar 13, 2008, 09:09 PM
Is there any information on how to actually use them in the context of an iPhone application?

I've written a post on how to integrate FMDB here: http://blog.saers.com/archives/2008/03/14/sqlite-for-iphone-sdk/

realberen
Mar 13, 2008, 09:11 PM
At this point there is no way to sync any data from your own apps to a desktop machine.

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 :-)

mysticwhiskey
Mar 31, 2008, 11:00 PM
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.

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.

stark
Jun 22, 2008, 05:41 AM
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.

rastersize
Jun 22, 2008, 02:40 PM
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.

That's because your supposed to import UIKit/UIKit.h ;)

cleverapps
Aug 2, 2008, 01:24 AM
There's also an object oriented database for the iPhone: http://code.google.com/p/entropydb/

levous
Oct 8, 2008, 12:43 PM
There's also an object oriented database for the iPhone: http://code.google.com/p/entropydb/

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 :(