3

When the below function is called, I get the EXC_BAD_ACCESS crash. It looks like FMDB is having a problem interpreting the subject_id NSInteger as it makes it through the two NStrings and bombs when it hits this subject_id column in the WHERE clause.

- (void) saveAllData {

if(isDirty) {

    DrillDownAppAppDelegate *appDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    FMDatabase *database = [FMDatabase databaseWithPath:appDelegate.getDBPath];    

    if ([database open]) {

        [database executeUpdate:@"update Subject Set subject = ?, category = ? where subject_id = ?", 
        self.title, self.category_title, self.subject_id];

        [database close];
    }

    isDirty = NO;
}

//Reclaim all memory here.
[title release];
title = nil;
[category_title release];
category_title = nil;

}

The problem is the same as I ran into in another post on FMDB insert problems and this boils down to something wrong with my subject_id member. I believe I am using a wrong declaration in the header. Here it is:

//
//  Subject.h
//  DrillDownApp

    #import <UIKit/UIKit.h>

    @interface Subject : NSObject {
        NSInteger subject_id;
        NSString *category_title;
        NSString *title;
    //    NSMutableArray *quotes;
        BOOL isDirty;
        //  BOOL isDetailViewHydrated;

    }

- (id) initWithPrimaryKey:(NSInteger)pk;
    @property (nonatomic, readwrite) BOOL isDirty;
    //@property (nonatomic, readwrite) BOOL isDetailViewHydrated;
- (void) addSubject;
- (NSInteger)getNextSubjectId;

    @property (nonatomic, assign) NSInteger subject_id;
    @property (nonatomic, copy) NSString * title;
    @property (nonatomic, copy) NSString * category_title;
    //@property (nonatomic, retain) NSMutableArray *quotes;
    //- (void) saveAllData;


    @end

(Note: I edited this majorly as I figured out the rest of it.)

jroyce
  • 2,029
  • 2
  • 22
  • 45
  • You are setting title value and category title value to the Subject object. But where you are saving the object to the database? – Ilanchezhian Jan 27 '12 at 07:40
  • I updated this to include the saveAllData function that saves the object to the database. But the problem is it doesn't even make it there as the data doesn't stay with the object. I think my problem is that I am not calling the setTitle and setCategory_Title function correctly as these methods never get touched when running in debugger... – jroyce Jan 27 '12 at 18:44

2 Answers2

8

Ok I solved it. FMDB will not work using Integers. You must convert them into Numbers. I found this done in the examples on FMDB doc and there is never an int being passed through an executeUpdate statement.

So in my example above the way I fixed this was with the following:

[database executeUpdate:@"update Subject Set subject = ?, category = ? where subject_id = ?", self.title, self.category_title, [NSNumber numberWithInt:self.subject_id]];

I wished this was better documented, but oh well.

jroyce
  • 2,029
  • 2
  • 22
  • 45
  • For the record, it didn't seem to keen on my BOOL, either, so numberWithBool:bool saved the day for me. Thank you for the answer. – eddieroger Aug 01 '12 at 14:12
  • Tnx, updated link : https://ccgus.github.io/fmdb/html/Classes/FMDatabase.html#//api/name/executeUpdate: – Mike.R Aug 26 '15 at 06:31
2

I don't know from where you are calling the method saveAllData. Still your question lacks requirement so anyone to answer.

Also, I have found one problem with your code.

Instead of the following code,

//Update the value.
[sub setTitle:txtSubject.text];
[sub setCategory_title:txtCategory.text];

use the following code

//Update the value.
sub.title = txtSubject.text;
sub.category_title = txtCategory.text;

so that the strings have the copy property. If you override the setter method, then you might need to explicitly copy the string. Else it will just assign the value without copy.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • Thanks for your post. I fixed the above but it didn't resolve getting value assigned to this object that then carries over to the DrillDownDelegate class where the saveAllData method is fired. I know this method is firing from NSLog statements, but it is not finding any Dirty records. (ps. if you need any further data to be able to help me, let me know. TKU) – jroyce Jan 30 '12 at 02:28
  • I majorly edited the above and changed the title so it clearly states what I am running into now. – jroyce Jan 30 '12 at 04:33