1

I am using FMDB and I am inserting data into my database with:

NSString *sql = [NSString stringWithFormat:@"insert into \"main\".\"DailyCycles\" ( \"objectID\",\"UserID\", \"Calories\") values ( '%@', '%@', %@)",
                 dailyTotal.objectId,
                 dailyTotal.userID,
                 dailyTotal.caloires,
                 ];

However, I don't want duplicates in the database. I've tried "update into" but then it won't insert new data. Is there a way to ensure that data is inserted into the database when there is no existing record while just updating any existing records?

Eric Chuang
  • 1,017
  • 9
  • 28

1 Answers1

0

See this question: INSERT IF NOT EXISTS ELSE UPDATE?

By the way, be very careful when building your SQL queries like that. NSString stringWithFormat is not intended for such uses, and you might be opening the way to SQL injections. Here's how you should probably be doing it:

[db executeUpdate:@"insert into main.DailyCycles ( objectID, UserID, Calories) values (?, ?, ?)", dailyTotal.objectId, dailyTotal.userID, dailyTotal.calories];
Community
  • 1
  • 1
Romain
  • 3,718
  • 3
  • 32
  • 48