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 Answers 2
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
1 Comment
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;
}
sqlite3_open_v2call.sqlite3_open_v2("SoundLib", &dbConnection,...is not just fishy, it's wrong