如何在sql中与convert一起使用占位符?

h43kikqp  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(238)

这是我的密码:

const result = await connection.query("INSERT INTO myTable ([ID],[ClassId],[Active],
[LastUpdateDateTime],[LastUpdateUser],[Number],[ExternalId],
[MaterialDefinitionId],[CompanyId],[IsBlanket],[Type],[Subtype],
[CreatedDate],[ValidFromDate],[ValidToDate],[OrderedQuantity],
[DeliveredQuantity],[ReservedQuantity],[UnitOfMeasurement],[Status],[Note])

VALUES (?,?,?,CONVERT(DATETIME, ? , 102),?,?,?,?,?,?,?,
?,CONVERT(DATETIME, ? , 102),CONVERT(DATETIME, ?, 102),
CONVERT(DATETIME, ? , 102),?,?,?,?,?,?)" , 

['100000061', 'Order' , 1, 
'2020-01-01 11:11:11' , 'xxx' , 123 , '9901177998' , '100000003' ,
'100000012' , 0 , 'xxx', 'xxx', '2020-01-01 11:11:11' , 
'2020-01-01 11:11:11' , '2020-01-01 11:11:11' , 2000.0 , 
0.0 , 0.0, 'xxx' , 'Created' , 'xxx']);

正如您所看到的,我正在尝试插入时间,但出现了以下错误:“?”附近的requesterror:语法不正确。
有人知道解决办法吗?
谢谢!

um6iljoc

um6iljoc1#

您需要为中的参数使用数组 connection.query 函数来设置值。你可能只在你的案子里设置了第一个。
支票:https://www.mysqltutorial.org/mysql-nodejs/insert/
应该是这样的:

let stmt = `INSERT INTO todos(title,completed)
            VALUES(?,?)`;
let todo = ['Insert a new row with placeholders', false];

// execute the insert statment
connection.query(stmt, todo, (err, results, fields) => {
  if (err) {
    return console.error(err.message);
  }
  // get inserted id
  console.log('Todo Id:' + results.insertId);
});

在您的情况下,您应该尝试:

const result = await connection.query("INSERT INTO myTable ([ID],[LastUpdateDateTime]) VALUES (? , CONVERT(DATETIME, ? , 102))", [1, '2020-01-01']);

相关问题