0

Am making an iOS apps with an embedded SQLite database. So I make my DB in a SQLite administrator and drag it to my Xcode project, like in the tutorial says. When I try open my DataBase, I get this error: "out of memory". Dont know if it is a SQLite bug or something, but my file is really small to get a memory problem.

This is my code to init my DataBase:

- (id)initWithPath:(NSString *)path {
if (self = [super init]) {
    BOOL success;
    NSError *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"SoundLib_DB.sqlite"];

    if ([fileManager fileExistsAtPath:dbPath] == NO) {
        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"SoundLib_DB" ofType:@"sqlite"];
        [fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];
    }

    success = [fileManager fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    sqlite3 *dbConnection;
//Here is when I get the error, at trying to open the DB
    if (sqlite3_open_v2("SoundLib", &dbConnection, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
        NSLog(@"[SQLITE] Unable to open database!");
        NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
        return nil;
    }
    database = dbConnection;
}
return self;

}

2
  • 1
    You're not using the actual file path of your database in the sqlite3_open_v2 call. Commented Jan 23, 2013 at 16:19
  • 2
    Yes, that sqlite3_open_v2("SoundLib", &dbConnection,... is not just fishy, it's wrong Commented Jan 23, 2013 at 16:21

2 Answers 2

5

Error may be because you need to pass databasepath as UTF8String. I see you are passing "SoundLib". You can not directly pass like that.

 if (sqlite3_open([dbPath UTF8String], &databaseHandle) == SQLITE_OK)
{
   //dbPath is your full database path you getting above
}

P.S. Also have dbpath as const char

const char *dbpath
Sign up to request clarification or add additional context in comments.

1 Comment

Anytime. Word of advice: Do keep habit of reading official Apple docs. Relying only on tutorials will build a weak foundation of your coding skills.
3

Change your opening process so that it looks like this... It works for me ;) I guess your main mistake is that you forgot the UTF8String....

sqlite3 *database;
int result = sqlite3_open([dbPath UTF8String], &database);
if (result != SQLITE_OK) {
    sqlite3_close(database);
    NSLog(@"Error opening databse");
    return;
}

1 Comment

I mark as correct the previous one, just because I read it first, but this works too man. Thank you.

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.