SQLite: DB error, "out of memory"

SpokaneDude picture SpokaneDude · May 5, 2011 · Viewed 8.7k times · Source

I am using FMDB to create and add a record to a d/b. The method to create the d/b is:

//-----------------------    checkIfDatabaseExists    -----------------|
+ (void) openCreateDB  {

    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  // Get the path to the database file
    NSString *documentPath = [searchPaths objectAtIndex:0];
    NSString *databasePath = [documentPath stringByAppendingPathComponent:@"ppcipher.s3db"];
    NSLog(@"d/b path: /%@", databasePath);

    char * errmsg = nil;   

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:databasePath error:NULL];  //  <------------  delete d/b  TESTING ONLY! 

    BOOL fileExists = [fileManager fileExistsAtPath:databasePath];
    if(!fileExists)  {
        FMDatabase* _db = [FMDatabase databaseWithPath: databasePath]; 

        if (![_db open]) {
            NSLog(@"Could not open/create database");
        }

        [_db executeUpdate:@"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
         @"card_type TEXT, code_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)"];

        if(errmsg != nil)
            NSLog(@"error: %s", errmsg);  //  DEBUGGING ONLY!  (REMOVE when done!)
    }
    return;
}

Which causes NO errors. However, when I next do one (1) "insert" after the "open", I get an error from FMDB saying DB Error: 7 "out of memory". And every sql statement after this, I get the same error (only the creation of the d/b gave no errors!). Here is the code for the insert:

//---------------------    addRecordToDatabase    ----------------------|
+ (void)addRecordToDatabase: (ZBarSymbol *)symbol  {


    FMDatabase* _db = [FMDatabase sharedFMDatabase];

    [_db setLogsErrors:1];  //  log all of the SQLite d/b errors

    [_db executeUpdate: @"INSERT INTO CardData (card_id, card_name, code_val) VALUES (?, ?, ?)", symbol.data, @"Test Card", symbol.typeName, nil];

}

This is a very small d/b with minimal data. I ran Inspector and nothing was out of the ordinary. Any suggestions would be greatly appreciated.

Answer

suprandr picture suprandr · May 5, 2011

I think you should just call[_db open] in your code, that should fix it. It seems that the "out of memory error" in FMDB means also error caused by missing table(s) or connection not open.