Nodejs和MySQL,如何在MySQL INSERT语句中使用NodeJs变量

时间:2018-03-19 17:15:40

标签: mysql node.js

所以基本上,我有这个JavaScript变量存储这个特定的值,我需要将这个变量放在MySQL Query语句中。

源代码:

var Pollution_Reading = 25;
DatabaseConnection.query('INSERT INTO Air_Pollution_Record (Air_Pollution_Reading) VALUES ('"Pollution_Reading"')');

我已经尝试了所有我知道的方式,但我仍然无法将变量所持有的值插入到数据库中。我该怎么办?

3 个答案:

答案 0 :(得分:1)

DatabaseConnection.query('INSERT INTO Air_Pollution_Record (Air_Pollution_Reading) VALUES ('+Pollution_Reading+')');

答案 1 :(得分:0)

尝试这样做:

//reading  post data
const useremail = req.body.useremail;
const password = req.body.password;
//save into db start
var sql = "INSERT INTO `users` (`UserName`, `UserPassword`) VALUES ('"+useremail+"','"+ password+"')";

答案 2 :(得分:0)

您可以尝试这种模板文字的格式。 为您提供简单干净的代码。

const sql = `INSERT INTO query_form (name, email, query_title, query_description) VALUES ('${name}', '${email}', '${query_title}', '${query_description}')`;