Skip navigation
This discussion is archived

SQLite database on iPhone

2851 Views 16 Replies Latest reply: Nov 13, 2008 7:36 PM by coolguy1981 RSS
1 2 Previous Next
kaiuskal Level 1 Level 1 (5 points)
Currently Being Moderated
May 28, 2008 10:46 AM
I done all my development for my SQL on the simulator, and I have my app writing and reading from my DB seamlessly.

In fact, very few lines of code go quite far. In my Apple computer I am able to find my database and verify that all is working properly (under library/application support/iphone simulator/user/applications/myapp/documents)

BUT...

I know have moved the identical code to the iphone, and my app is UNABLE to read my database for its default configuration.

My code to find the DB in question comes from the Apple demos:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyDB.sql"];

Questions:
1) When I "build" my app for the iphone, and not the simulator, and my default DB is part of my project, is it copied to the iphone?
2) How can I see where it goes?
3) Why is it that what works on my simulator, does not work on the iPhone?

Thanks!
  • EagerEyes Calculating status...
    Currently Being Moderated
    May 28, 2008 12:11 PM (in response to kaiuskal)
    Have you looked at the SQLiteBooks example? They copy the database to the Documents directory before opening it because the db in the bundle can't be written to. That might be necessary even for a simple select to get a lock on the file. The code in that example is very useful, and there are some good comments there as well.
  • EagerEyes Level 1 Level 1 (65 points)
    Currently Being Moderated
    May 28, 2008 12:58 PM (in response to kaiuskal)
    I'm sorry, I didn't read your posting properly. The only thing that comes to mind right now is a problem with the memory you use for the statement. Maybe you could try a const char *myQuery = "SELECT ..."; to be sure that the memory isn't the problem. Or perhaps something overwrites your database pointer before you get to the prepare call.
  • EagerEyes Level 1 Level 1 (65 points)
    Currently Being Moderated
    May 28, 2008 5:27 PM (in response to kaiuskal)
    I'm using it and it has worked fine on both the simulator and the device so far. Maybe there was just some weird bug that interfered with your code? Glad it works now!
  • gkstuart Level 2 Level 2 (225 points)
    Currently Being Moderated
    May 28, 2008 9:08 PM (in response to kaiuskal)
    SQLite works fine for me in the simulator. I have not been admitted to the developer program yet so I have no way of doing device testing with FW 2.0 yet... and I suspect there may be a good number of people in this situation. Just my hunch...
    MBP Intel 2.4GHz, Mac OS X (10.5.3)
  • Steve Patt Level 1 Level 1 (30 points)
    Currently Being Moderated
    May 28, 2008 10:20 PM (in response to kaiuskal)
    Any recommendations on the best things to read to get up to speed on SQLite?
    iMac, Mac OS X (10.5.2)
  • gkstuart Level 2 Level 2 (225 points)
    Currently Being Moderated
    May 28, 2008 10:29 PM (in response to Steve Patt)
    [http://www.sqlite.org>

    also, there is a SQLite example in the SDK (SQLBooks I think? something like that) which is quite good you should take a look at.

    Hope this helps!
    MBP Intel 2.4GHz, Mac OS X (10.5.3)
  • HeoU Calculating status...
    Currently Being Moderated
    May 29, 2008 12:08 AM (in response to kaiuskal)
    It works fine on both for me. I'm actually on beta 5.

    HeoU
    iMac, Mac OS X (10.5.2)
  • jaekwon Level 1 Level 1 (5 points)
    Currently Being Moderated
    May 29, 2008 5:15 PM (in response to kaiuskal)
    I found that (at least for beta 5) there were in fact some differences in SQLite3 behavior.
    So I had code that worked in the simulator but not on the IPhone.

    I don't remember what the issue was, it could be one of the following:
    - check your bind variable name, and make sure the prefix is right and consistent ('$', '%' etc)
    - some ROLLBACK statements can throw an exception
    - i think i figured out the problem by getting the specific error message. use sqlite3_errmsg like this: [NSString stringWithCString:sqlite3_errmsg(self->database)]
    - make sure the database schema in the iphone documents directory is consistent with your template .sql file.

    - Jae
    Mac OS X (10.5.2)
1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.