-
Notifications
You must be signed in to change notification settings - Fork 0
/
008-更新数据.js
44 lines (41 loc) · 918 Bytes
/
008-更新数据.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* @Date: 2023-06-06 19:34:12
* @LastEditors: with-the-winds
* @LastEditTime: 2023-06-06 19:34:23
* @Description: 头部注释
* @FilePath: \study-node-mysql\008-更新数据.js
*/
const mysql = require('mysql')
// 创建连接
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root123',
database: 'nodemysql1'
})
// 连接数据库
db.connect(err => {
if (err) throw err;
console.log('mysql connected ...');
})
// 更新数据
/**
* 语法:
* UPDATE table_name
* SET field1=new-value1, field2=new-value2
* [WHERE Clause]
* 解释:
* Clause 是条件,可以通过对应条件查询
*/
const updateSql = `
UPDATE goods
SET numbers = 100, update_by = 'root2'
WHERE name = '苹果'
`
db.query(updateSql, (error, result) => {
if (error) {
console.error('Error update data:', error);
return;
}
console.log('update successfully!');
})