【发布时间】:2021-12-13 19:39:59
【问题描述】:
我是 MySQL 新手,以前使用 PSQL 的方式与使用 MySQL 相同,但以下代码出错。
return await db
.raw(
`INSERT INTO users(firstName,
lastName,
email,
password,
type,
addressLine1,
addressLine2,
addressLine3,
mobileNumber,
townCity,
postcode)
VALUES (
'${firstName}',
'${lastName}',
'${email}',
'${hashedPassword}',
'${mobileNumber}',
${type},
'${addressLine1}',
'${addressLine2}',
'${addressLine3}',
'${townCity}',
'${postcode}');
SELECT * from users WHERE id = LAST_INSERT_ID()`,
)
.then(() => ...)
返回以下错误。
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "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 'SELECT * from users WHERE id = LAST_INSERT_ID()' at line 24",
sql: 'INSERT INTO users(firstName, \n' +
' lastName, \n' +
' email, \n' +
' password, \n' +
' type,\n' +
' addressLine1, \n' +
' addressLine2, \n' +
' addressLine3, \n' +
' mobileNumber,\n' +
' townCity, \n' +
' postcode)\n' +
' VALUES (\n' +
" 'Jane',\n" +
" 'Doe',\n" +
" 'janedoe@email.com',\n" +
" 'password', \n" +
" '07123456789',\n" +
' 100, \n' +
" '1', \n" +
" 'Test road', \n" +
" 'Westminster', \n" +
" 'London', \n" +
" 'L1 1TG'); \n" +
' SELECT * from users WHERE id = LAST_INSERT_ID()'
}
我怀疑它与模板文字有关,但不确定解决此问题的最佳方法,它在 PSQL 中运行良好(不包括最后一行)。有没有更好的使用原始 SQL 的方法,我对所有高级 ORM 命令都不感兴趣。
【问题讨论】:
标签: javascript mysql node.js express knex.js