Can't update FMDB

Discussion in 'iOS Programming' started by scubadivingfool, Apr 19, 2012.

  1. scubadivingfool macrumors newbie

    Joined:
    Apr 19, 2012
    #1
    Hi All,

    I have a SQLite3 database with FMDB. Everything works in the sense of inserting but when I try and add detail to an existing database it crashes. I end up with the following code in error with "EXC_BAD_ACCESS (code=2, address=0x4)

    Can anyone help with this problem?

    Thanks in advance

    Code:
    BOOL success = [db executeUpdate:[NSString stringWithFormat:@"UPDATE customers SET firstname = '%@', lastname = '%@', date = '%@' where id = %d",customer.firstName,customer.lastName,customer.customerId,customer.Date]];
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Break It Down.

    You're doing two distinct things:
    1. stringWithFormat:
    2. executeUpdate:

    Step 1 is to just make the formatted string. Store it in a variable. Set a breakpoint. Step through line by line. Exactly what is the contents of the formatted string? NSLog it and post it.

    If stringWithFormat: crashes, then you've identified the cause of the problem. So step 2 is to break the long string with multiple items down into separate pieces. Make one formatted string for each individual item. Store each one in a separate variable. Which one of them crashes?

    Since you didn't show what any of the types for the formatted properties are, maybe one of them isn't an object, but it should be (to go with %@). Conversely, maybe something is an object but shouldn't be (less likely to be fatal, but still worth checking). If I had to guess, the "address=0x4" in your error message looks suspiciously like a small integer number being misinterpreted as an object pointer.

    If the formatted string builds correctly, does the content look valid for the SQL structure you have? Since you didn't show what any of the field types are, consider doing that. Without more detail, all anyone can do is guess.
     
  3. scubadivingfool thread starter macrumors newbie

    Joined:
    Apr 19, 2012
    #3
    Sorry about not showing the code. see below.

    Thanks for your help. very much appreciated. Just to let you know I am new to IOS and haven't done any SQL in many years.

    This app does work when I jet have the fast name and last name and can update it. The problem occurs when date is added

    Code:
    BOOL success = [db executeUpdate:[NSString stringWithFormat:@"UPDATE customers SET firstname = '%@', lastname = '%@', date = '%@' where id = %d",customer.firstName,customer.lastName,customer.customerId,customer.Date]];
    Code:
    [db updateCustomer:customer];
    Code:
    [self.delegate addCustomerViewController:self didEditCustomer:self.customerToEdit];
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    You still haven't shown the types of the properties.

    And it doesn't look like you tried breaking it down and using breakpoints, either. Because all you did was repost the original code fragment, and add two isolated lines.

    I've hilited several potential problems in red.

    The spelling errors suggest a lack of focus on the details. Or too much haste.

    The order of customer.X properties doesn't seem to match the order implied by the SQL statement. Look closely at the order of fields in the SQL. Write them down on paper, numbered, in a single column. Then carefully compare to the order of parameters as given to be formatted.

    Furthermore, unless Date is an int type and customerId is an object type, the format string is wrong. I'm just guessing, though, because you haven't said what any of the types are.


    I don't know what to do with these lines of code.
    They're isolated.
    Without context.
    They show no types.
    They define no methods or functions.
    What explanatory or descriptive value do they add?
    None that I can see.
     

Share This Page