【转】nodejs更新mysql数据库信息

转载地址:https://www.yiibai.com/mysql/nodejs-update.html

在本教程中,您将学习如何从node.js应用程序更新MySQL数据库中的数据。

要从node.js应用程序更新数据,请使用以下步骤:

要连接到MySQL数据库,我们将使用以下config.js模块,其中包含MySQL数据库服务器的必要信息,包括主机,用户,密码和数据库。

let config = {
  host    : 'localhost',
  user    : 'root',
  password: '123456',
  database: 'todoapp'
};

module.exports = config;

Js

更新数据示例

以下update.js程序根据特定的ID来更新托管的状态。

let mysql = require('mysql');
let config = require('./config.js');

let connection = mysql.createConnection(config);

// update statment
let sql = `UPDATE todos
           SET completed = ?
           WHERE id = ?`;

let data = [false, 1];

// execute the UPDATE statement
connection.query(sql, data, (error, results, fields) => {
  if (error){
    return console.error(error.message);
  }
  console.log('Rows affected:', results.affectedRows);
});

connection.end();

Js

在这个例子中,我们在UPDATE语句中使用了占位符(?)。

当通过在连接对象上调用query()方法执行UPDATE语句时,以数组的形式将数据传递给UPDATE语句。 占位符将被数组中的值替换为数组。 在这个例子中,将id1的那条记录的completed列将被设置为false

回调函数的results参数有affectedRows属性,返回UPDATE语句更新的行数。

在执行程序之前,请查看todos表中id1的行记录信息:

mysql> SELECT * FROM todos WHERE id = 1;
+----+-------------------------------+-----------+
| id | title                         | completed |
+----+-------------------------------+-----------+
|  1 | Learn how to insert a new row |         1 |
+----+-------------------------------+-----------+
1 row in set (0.00 sec)

Shell

现在,我们来运行上面update.js程序。

F:\worksp\mysql\nodejs\nodejs-connect>node update.js
openssl config failed: error:02001003:system library:fopen:No such process
Rows affected: 1

Shell

程序返回一条消息,指示受影响的行数为1, 我们可以在数据库中再次查看它,如下所示:

mysql> SELECT * FROM todos WHERE id = 1;
+----+-------------------------------+-----------+
| id | title                         | completed |
+----+-------------------------------+-----------+
|  1 | Learn how to insert a new row |         0 |
+----+-------------------------------+-----------+
1 row in set (0.00 sec)

Shell

您可以看到,completed列中的值已更新为0,在node.js中为false

在本教程中,我们向您展示了如何从node.js应用程序更新MySQL中的数据。

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/mysql/nodejs-update.html
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Node.js操作MySQL数据库的示例代码: 1.安装mysql模块 ```shell npm install mysql ``` 2.连接MySQL数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); }); ``` 3.创建数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); connection.query('CREATE DATABASE test', (err, result) => { if (err) throw err; console.log('Database created!'); }); }); ``` 4.创建表 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))'; connection.query(sql, (err, result) => { if (err) throw err; console.log('Table created!'); }); }); ``` 5.插入数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'INSERT INTO customers (name, address) VALUES ("Company Inc", "Highway 37")'; connection.query(sql, (err, result) => { if (err) throw err; console.log('1 record inserted'); }); }); ``` 6.查询数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'SELECT * FROM customers'; connection.query(sql, (err, result) => { if (err) throw err; console.log(result); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值