mysql - 如何使用 NodeJS 更新数据?

标签 mysql node.js

我想将结果预测更新到数据库 mysql,但数据无法保存在数据库中。

const periode = _.get(row, "periode", new Date());
const prediction =
          b0 +
          b1 * Number.parseFloat(_.get(row, "x1", 0)) +
          b2 * Number.parseFloat(_.get(row, "x2", 0));

        const sql = `UPDATE performance SET prediction_result="${prediction}" WHERE periode="${periode}"`;

        db.query(sql, (error, results, fields) => {
            if (error) {
              console.error('An error occurred while executing the query')
              throw error
            }
          })

我遇到这样的错误:

throw err; // Rethrow non-MySQL errors
ReferenceError: connection is not defined

你能帮我解决这个问题吗?

最佳答案

你的代码中应该有这样的东西。

var mysql = require('mysql');

var db= mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

db.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

关于mysql - 如何使用 NodeJS 更新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463834/

相关文章:

javascript - 如何在客户端使用 create-react-app 设置整个应用程序的环境变量

node.js - node_modules 中 .bin 文件夹的用途是什么?

linux - Loggly 无法通过 Pi 上的 Winston 工作

nginx - node.js 准备好用于生产了吗?

php - 如何存储两个客户相关?

mysql - foreach 不打印 cakephp 中的查询结果

mysql - 如果没有结果则返回指定值

javascript - 如何在 NodeRED 数组中收集从 angularJS 发送的字符串?

php向mysql数据库添加错误值

c# - 将硬编码数据传输到数组