1

I have an iPad app that is using a sqlite database using FMDB as a wrapper. I am able to successfully query data from the database; however when I attempt to insert data using executeUpdate, the method fires correctly (i.e. I get no errors and it returns YES) but nothing is actually inserted into the database. I try to immediately retrieve the object I just inserted and it is null. I also try to look at the number of rows in the table I inserted to and the table is empty. Any idea what I'm doing wrong? Here's some of the code:

--init method of the class that handles all my db work

    -(NSString *)getDbPath {    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        return [documentsDirectory stringByAppendingPathComponent:@"iRecipeBox.db"];
     }

    -(id)init {
        self = [super init];
        if(self) {
           database = [FMDatabase databaseWithPath:[self getDbPath]];
           [database open];
        }

       return self;
     }

--App delegate code that moves my db into the docs directory

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString *docsPath = [documentsDirectory stringByAppendingPathComponent:@"iRecipeBox.db"];

        if ([fileManager fileExistsAtPath:docsPath] == NO) {
            NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"iRecipeBox" ofType:@"db"];
            [fileManager copyItemAtPath:resourcePath toPath:docsPath error:&error];
         }
         return YES;

}

--Code to insert data into one of the tables

    recipeID = [NSNumber numberWithInt:newRecipeID];
    [database beginTransaction];
    NSArray *newRow = [[NSArray alloc] initWithObjects:recipeID,title, description, picFile, prepTime, cookTime, ovenTemp, nil];
    NSString *sql = @"insert into recipes (id, name, descr, picFile, prepTime, cookTime, ovenTemp) values (?, ?, ?, ?, ?, ?, ?)";
    NSLog(@"Before update, the number of args is %i", [newRow count]);

    if([database executeUpdate:sql withArgumentsInArray:newRow]) {
        [database commit];
        NSLog(@"the insert worked");
    } else {
        [database rollback];
        NSLog(@"the insert failed");
    }

1 Answer 1

1

[FMDatabase databaseWithPath:] returns an autoreleased object, so you're going to want to retain that if you're not using ARC.

Check [database lastError] right after the executeUpdate:, and see if it says anything.

And finally- there's no point in using a transaction if it's just a single insert.

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

Comments

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.