Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

SQLite database on iPhone

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!

Posted on May 28, 2008 10:46 AM

Reply
16 replies

May 28, 2008 12:08 PM in response to kaiuskal

In fact the following code works on my MAC iphone simulator, but not on my iPhone:

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

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK){

sqlite3_stmt *statement;
strcpy (gMyAlQuery,"SELECT * FROM SysDefaults WHERE PK = 1");
if (sqlite3 preparev2(database, gMyAlQuery, -1, &statement, NULL) == SQLITE_OK)
...

THE ABOVE "if" is the one failing on the iPhone, not the first "if" so that means that my code IS finding the DB on the iphone but failing on the sqlite3 preparev2 line on the iPhone.

Has anybody seen this as a bug - or am I getting away with something on the simulator that is not kosher on the iPhone?

May 28, 2008 12:50 PM in response to EagerEyes

Hi,

Thanks for your response. In fact, I have patterned my code around that example, and yes, I do use the createEditableCopyOfDatabaseIfNeeded code up front...

So this would not seem to be the problem. The line that compiles the query seems to be the problem while running on the iphone, while it works just fine on the simulator.

Has anybody had problems with the compiling of the query statement while running on the iphone?

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.

May 28, 2008 5:14 PM in response to kaiuskal

The problem has been solved with NO code changes on iPhone version 6 and SDK 6.

I just recompiled everything and - bingo!

Which leads me to the next question... Who is using SQLite around here if nobody had brought this issue up?

For such a powerful feature in the SDK I am surprised to see how few of you are using it for setting up default configurations, saving data, etc.

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

SQLite database on iPhone

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.