Search code examples
iossqliteinsertinsert-updatefmdb

Error when inserting value into fmdb


What I coded is below:

[db executeUpdate:@"INSERT INTO usercart (id, productid, name, description, campaignid, startdate, enddate, martid, martname, price, update) VALUES (?,?,?,?,?,?,?,?,?,?,?)",(int)cartItem.databaseId,(int)cartItem.itemId,cartItem.itemName,cartItem.itemDescription,(int)cartItem.campaignId,cartItem.campaignStartDate,cartItem.campaignEndDate,(int)cartItem.martId,cartItem.martName,(int)cartItem.itemPrice,cartItem.lastUpdate]

And I got an erorr and message below :

DB Error: 1 "near "update": syntax error"
DB Query: INSERT INTO usercart (id, productid, name, description, campaignid, startdate, enddate, martid, martname, price, update) VALUES (?,?,?,?,?,?,?,?,?,?,?)
DB Path: file:///var/mobile/Containers/Data/Application/2F08F458-BAB3-4523-B942-68ABF67CACF6/Documents/HaffleMonster.sqlite
Error Domain=FMDatabase Code=1 "near "update": syntax error" UserInfo=0x170e65740 {NSLocalizedDescription=near "update": syntax error}
near "update": syntax error

I don't know why. I think i am right with syntax of sqlite and fmdb Can you explain why?


Solution

  • I don't think you can use update as a variable name since it's used by SQL. Just like you can't use insert or into as variable names.

    You either need to change the variable name or encapsulate it with [], like this [update]. So that it reads:

    ...price, [update]) VALUES (?,?,?...