1

When I test my app on the simulator everything works. But when I test the app on my iPhone, it doesn't load from the db.sqlite3 data base.

I tried every solution from the internet. I cleaned the app. I added the database to the build phrases - copy bundle resources etc. Can you help me? Is there any fails in my coding?

-(void)openDataBase{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    dbPath = [documentsDirectory stringByAppendingString:@"db.sqlite3"];

    //load it
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *pathInRessource = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"];

    //exist it?
    if (![fileManager fileExistsAtPath:dbPath]) {
        [fileManager copyItemAtPath:pathInRessource toPath:dbPath error:nil];
    }

    //open database
    int result = sqlite3_open([dbPath UTF8String], &db);
    if (result != SQLITE_OK) {
        sqlite3_close(db);

        return;
    }

    //if ok
    NSLog(@"Data base opened");

}
11
  • Well, you have a bunch of code there. When you step through it, when do you notice something going awry? And what error messages are you ignoring? Commented Jul 3, 2014 at 20:50
  • Hey on the simulator i got the message "Data base opened" but when i run it on the iphone i get no message. I think, the iphone can't find the path. But i don't know how to solve it. Commented Jul 3, 2014 at 21:09
  • I'd specifically look at what sqlite3_open() returned and check that against sqlite.org/c3ref/c_abort.html Commented Jul 3, 2014 at 21:09
  • 1
    My guess is you get the wrong path (that would explain why it works in the simulator, where it is different), it should look like file:///var/mobile/Applications/...id.../Documents/YourDatabase.sqlite. Try copying the code to find a path from another source. Commented Jul 3, 2014 at 21:13
  • 1
    According to the docs it returns a BOOL, not a string. You should probably also not be swallowing the error like you are using nil Commented Jul 3, 2014 at 21:40

2 Answers 2

2

At the time of opening database you can call this with its else case to get the issue.

if (sqlite3_open([getDBPath UTF8String], &sqlhandle) == SQLITE_OK)
{
     NSLog(@"Database Open!");
}
else
{
    NSLog(@"Select err: %s",sqlite3_errmsg(sqlhandle));
}

You will be able to get proper error message using this

Sign up to request clarification or add additional context in comments.

1 Comment

Now i debugged with the iOS simulator and with my device. Then I compared the results. The only different is my integer result. On simulator result = 0. On device = 4. On iOS simulator its going from the if statement to the NSLog. On device its going from if to close and return. I don't understand that at the moment.
1

I am wondring where you getting stuck, I am doing same way and its working fine to me.

BTW I have updated your code, how I am handling, please check let me know if find and trouble in this.

//My DB variable
static sqlite3 *database;

-(void)openDataBase{
if (database == NULL) 

    sqlite3 *mDatabse;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    dbPath = [documentsDirectory stringByAppendingString:@"db.sqlite3"];

    //load it
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *pathInRessource = [[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite3"];

    //exist it?
    if (![fileManager fileExistsAtPath:dbPath]) {
        [fileManager copyItemAtPath:pathInRessource toPath:dbPath error:nil];
    }

    if (sqlite3_open([path UTF8String], &mDatabse) == SQLITE_OK) {   
            NSLog(@"Database Successfully Opened");
            database = mDatabse;


        } else {
            NSLog(@"Error in opening database");
            mDatabse = NULL;
        }

}

9 Comments

When you say "My Class variable", do you actually mean an instance variable?
yeah either you can declare it in header or implementation file. sorry if it confusing you, i just updated
Well, I wasn't confused, but whether a class- or instance-variable is used will have an impact on the number of instances of this class that can be used simultaneously. You appear to have made the worse choice.
I agree about the impact of class and instance variable, but I use this variable to check database then I have one local variable with that I do database operation, still If you find It can be improved, I would welcome.
That's very easy; make it an instance variable in a private category of the class: @implementation Database () { sqlite3 *_database; } ... @implementation Database { ... }. This allows multiple instances of this class to exist, which could prove very useful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.