0

Inserting new items to the table works perfectly, updating however has no effect.

I am using the following statement to update my db.

BOOL success = [fmDB executeUpdate:@"UPDATE activities SET type = ?, author = '?', 
                time = '?', location = '?', node = '?', nodeID = ? 
                WHERE itemid = ?", [NSNumber numberWithInt:itemType], author, 
                time, location, node, [NSNumber numberWithInt:nodeID], 
                [NSNumber numberWithInt:itemID], nil];

Everything that hasn't been wrapped as an NSNumber is an NSString (which all log out as expected).

I receive 0 errors when the command is run (the success bool returns TRUE)

The next time I read my db, the changes have not been made.

Any ideas?

Bongeh
  • 2,300
  • 2
  • 18
  • 30
  • I hope the database is not in the app bundle, but somewhere where it's writable... –  Jul 22 '13 at 12:24
  • The db is on the device - /var/mobile/Applications/*/Documents/FeedItems.db , like I said, I can insert to it, just not update items that already exist. – Bongeh Jul 22 '13 at 12:27
  • trace execution also seems to be logging out the correct statement being fired – Bongeh Jul 23 '13 at 08:41
  • http://stackoverflow.com/questions/16499614/ios-sqlite-fmdb-update-not-working – Mital Jul 23 '13 at 10:25
  • In the link you provide the solution was that the update query was not seperated by commas, mine were – Bongeh Jul 24 '13 at 09:25

2 Answers2

2

Okay so I got it to work.. some how..

 BOOL success = [fmDB executeUpdateWithFormat:@"UPDATE activites 
   SET type = %@, node = %@, time = %@, location = %@, author = %@, 
   nodeID = %@ WHERE itemid = %@", [NSNumber numberWithInt:itemType], 
   node, time, location, author, [NSNumber numberWithInt:nodeID], 
   [NSNumber numberWithInt:itemID], nil];

This didn't work until I'd removed the ' ' s from around the string objects.

Bongeh
  • 2,300
  • 2
  • 18
  • 30
0
 if([db open])
 {
    NSString *queryStr ;
    queryStr = [NSString stringWithFormat:@"Update activities SET type = ?, author = '?', 
            time = '?', location = '?', node = '?', nodeID = ? 
            WHERE itemid = ? ",[NSNumber numberWithInt:itemType], author, 
            time, location, node, [NSNumber numberWithInt:nodeID], 
            [NSNumber numberWithInt:itemID]];
    [db executeUpdate:queryStr];
    NSLog(@"UPDATE\n%@",queryStr);
  }
Mital
  • 241
  • 1
  • 5
  • You can't use '?' in [NSString stringWithFormat:], you have to use %@ if you want to do it that way. This is essentially doing nothing different from what I am doing except calling executeUpdate twice, no changes saved. – Bongeh Jul 23 '13 at 08:12