简体   繁体   中英

Node.js MySQL Query Error

I'm currently trying to implement a simple MySQL Database into my App. It should add a new User OR update if this User Exists. I'm working with Node.js mysql.

My code is:

INSERT INTO User ? ON DUPLICATE KEY UPDATE cost=VALUES(cost),
city=VALUES(city), romantik=VALUES(romantik),
kultur=VALUES(kultur), abenteuer=VALUES(abenteuer), 
familie=VALUES(familie), citytrip=VALUES(citytrip), 
kreuzfahrt=VALUES(kreuzfahrt)

and the output code is:

'INSERT INTO User `userId` = 566160350174682, `cost` = 400,
`city` = \'Berlin\', `romantik` = false,
`kultur` = true, `abenteuer` = true, `familie` = false,
`citytrip` = true, `kreuzfahrt` = false ON DUPLICATE KEY
UPDATE cost=VALUES(cost), city=VALUES(city), kultur=VALUES(kultur),
abenteuer=VALUES(abenteuer), familie=VALUES(familie),
citytrip=VALUES(citytrip), kreuzfahrt=VALUES(kreuzfahrt)'

This should work as far as I know but it always returns this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \\' userId = 566160350174682, cost = 400, city = \\'Berlin\\' at line 1

Can Somebody explain why this is happening and how I can solve this?

You have to use SET ..

Your query should be

INSERT INTO User SET `userId` = 566160350174682, `cost` = 400,
`city` = \'Berlin\', `romantik` = false, `kultur` = true, 
`abenteuer` = true, `familie` = false, `citytrip` = `kreuzfahrt` = false ON DUPLICATE KEY UPDATE cost=VALUES(cost), city=VALUES(city), kultur=VALUES(kultur),
abenteuer=VALUES(abenteuer), familie=VALUES(familie), citytrip=VALUES(citytrip), kreuzfahrt=VALUES(kreuzfahrt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM