Stephen
Stephen

Reputation: 4813

sqlite3_open - Can't open database?

I have the following statement in my code:

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

but it doesn't seem to be opening my database.

Any ideas ?

Upvotes: 3

Views: 7996

Answers (2)

DilumN
DilumN

Reputation: 2895

Simply use this filepath method

- (NSString *) filePath 
{    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

Upvotes: 0

Akusete
Akusete

Reputation: 10794

Without knowing anything else about your problem, one can only assume that your path is invalid.

Try using this path to see if it works

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths lastObject];
NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"mydb.sqlite"];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
     NSLog(@"Opened sqlite database at %@", databasePath);
    //...stuff
} else {
     NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
     sqlite3_close (database);
}

Upvotes: 6

Related Questions