1

I am trying to insert data into a database but I have it seems like it won't insert integer values, it is inputing strings. the app just crashes.

if I push the button that saves it into the database, with the textfields empty, the methods does insert data into the database, but with 0 values, so the method works.

the method I use is:

-(BOOL) insertIntoDatabase: (NSInteger)day: (NSInteger)month:(NSInteger)year:(NSInteger)hours:(NSInteger)minutes:(NSInteger)salary:(NSString *)extra
{
    FMDatabase *dbHandler = [FMDatabase databaseWithPath: [Utility getDatabasePath]];
    BOOL success;
    @try {
        [dbHandler open];

        success =  [dbHandler executeUpdate:@"INSERT INTO inputs (day, month, year, hours, minutes, salary, extra) VALUES (?,?,?,?,?,?,?);",
                         day, month, year, hours, minutes, salary, extra];

        [dbHandler close];
    }
    @catch (NSException *exception) {
        NSLog(@"error..");
    }
    @finally {
        return success;
    }
}

And this is the method I call from my viewController class

-(IBAction)enterToDatabase:(id)sender{

    InputClassDatabaseHandler *databaseMethods = [[InputClassDatabaseHandler alloc]init];
    BOOL accept;

    NSInteger day = [_day.text integerValue];
    NSInteger month= [_month.text integerValue];
    NSInteger year= [_year.text integerValue];
    NSInteger hours= [_hours.text integerValue];
    NSInteger minutes= [_minutes.text integerValue];
    NSInteger salary= [_salary.text integerValue];

    //hardcoded for testing...
    NSString *extraWork = @"No";

    accept = [databaseMethods insertIntoDatabase:day :month :year :hours :minutes :salary :extraWork ];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
uba2012
  • 373
  • 1
  • 3
  • 14
  • the app crashs - which error? btw, your code with "int accept = 1; if (accept ==1) { return success; }" looks strange... – NDY Aug 24 '12 at 14:24
  • there is no error, it just crashes when inputing integers. – uba2012 Aug 24 '12 at 14:26
  • the int accept thing was just something i did for testing... i have removed it. – uba2012 Aug 24 '12 at 14:28
  • i have also tried it like this:accept = [databaseMethods insertIntoDatabase:25 :8 :2012 :7 :15 :110 :extraWork ]; this doesnt work either – uba2012 Aug 24 '12 at 15:18
  • and with which type did you create your database fields? – NDY Aug 25 '12 at 06:20
  • i figured it out, i was using success = [dbHandler executeUpdate:@"INSERT INTO inputs (day, month, year, hours, minutes, salary, extra) VALUES (?,?,?,?,?,?,?);", day, month, year, hours, minutes, salary, extra]; but changed it to (%d, %d.....) and exuteUpdateWithFormat – uba2012 Aug 25 '12 at 21:38

1 Answers1

1

Always remember to pass any arguments to SQL query as an object. NSInteger in not an object, so put it in NSNumber object and it will work fine.

This should work for you:

-(BOOL) insertIntoDatabase: (NSInteger)day: (NSInteger)month:(NSInteger)year:
(NSInteger)hours:(NSInteger)minutes:(NSInteger)salary:(NSString *)extra
{

 FMDatabase *dbHandler = [FMDatabase databaseWithPath: [Utility getDatabasePath]];
 BOOL success = NO;
 if(![db open])
 {
   NSLog(@"Error");
   return NO;
 }
 [dbHandler beginTransaction];
 success =  [dbHandler executeUpdate:@"INSERT INTO inputs (day, month, year, hours, minutes, salary, extra) VALUES (?,?,?,?,?,?,?);",
                     [NSNumber numberWithInt:day], [NSNumber numberWithInt:month], 
                     [NSNumber numberWithInt:year], [NSNumber numberWithInt:hours],  
                     [NSNumber numberWithInt:minutes], [NSNumber numberWithInt:salary],          
                     [NSNumber numberWithInt:extra] ];

    [dbHandler commit];
    [dbHandler close];

 return success;
}
Ravi Raman
  • 1,070
  • 9
  • 16
  • thanks i had solved my problem some days ago, but this also worked – uba2012 Aug 27 '12 at 12:54
  • What was the problem that you were facing earlier? – Ravi Raman Aug 27 '12 at 12:56
  • I changed my method to updateWithFormat and could then pass and NSInteger with %d and as I have written above:) – uba2012 Aug 27 '12 at 17:39
  • But shouldn't I put a try and catch on the insertIntoDatabase method ? I am going with your way as it seems more professional. – uba2012 Aug 27 '12 at 17:53
  • The way I have written the code, it takes care of errors with the database. If you would like to be more cautious, check for the value returned in `success` variable and then "commit". Nice to be of help :) – Ravi Raman Aug 28 '12 at 04:50