# mysql 实现增删改查

  • 安装 mysql npm 包

    npm install mysql -S
    
    1
  • VSCODE 安装 code runner 右键 run code 即可运行代码

  • add.js 实现数据库增加功能

  • delete.js 实现数据库的删除功能

  • change.js 实现数据库的更新功能

  • search.js 实现数据库的查找功能

# 1.package.json

{
  "name": "mysql",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrapt": "^1.2.0",
    "koa": "^2.6.2",
    "mysql": "^2.16.0"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 2.create.js

const mysql = require("mysql")
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "test",
})
connection.connect((err) => {
  if (err) throw err
  console.log("连接成功...")
})
let createtable = `create table if not exists userinfo(
  id int(11) not null primary key auto_increment ,
  UserName varchar(50) not null,
  UserPass varchar(50) not null
)`
connection.query(createtable, function (err) {
  if (!err) {
    console.log("serinfo表")
  }
})
let insertsql = "insert into userinfo(UserName,UserPass) value?"
let value = [
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
]
connection.query(insertsql, [value], (err, res, fields) => {
  if (err) {
    console.log("添加失败", err.message)
    throw err
  }
  connection.destroy()
  console.log("添加成功")
})
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

# 2.delete.js

const mysql = require("mysql")
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "test",
})
connection.connect((err) => {
  if (err) throw err
  console.log("连接成功...")
})
let createtable = `create table if not exists userinfo(
  id int(11) not null primary key auto_increment ,
  UserName varchar(50) not null,
  UserPass varchar(50) not null
)`
connection.query(createtable, function (err) {
  if (!err) {
    console.log("serinfo表")
  }
})
let insertsql = "insert into userinfo(UserName,UserPass) value?"
let value = [
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
  [Math.ceil(Math.random() * 50), Math.ceil(Math.random() * 1000)],
]
connection.query(insertsql, [value], (err, res, fields) => {
  if (err) {
    console.log("添加失败", err.message)
    throw err
  }
  connection.destroy()
  console.log("添加成功")
})
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

# 3.updata.js

const mysql = require("mysql")
const connection = mysql.createConnection({
  localhost: "localhost",
  user: "root",
  password: "root",
  port: "3306",
  database: "test",
})
connection.connect()

let updateSql = "update userinfo set UserName = ?,UserPass = ? where id = ?"
let sqlValue = ["123sdsadf", "12344", 1]
connection.query(updateSql, sqlValue, function (err, res) {
  if (err) {
    console.log("更新失败", err.message)
    throw err
  } else {
    console.log("更新成功")
  }
})

connection.end()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 4.search.js

const mysql = require("mysql")
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  port: "3306",
  database: "test",
})

connection.connect()
let userSearchSql = "SELECT * FROM userinfo"
connection.query(userSearchSql, function (err, result) {
  console.log("查" + JSON.stringify(result))
})

connection.end()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16