七、NodeJs数据库管理

建表

MySQL程序可以使用PHP study集成工具。链接、操作数据库可以使用phpstudy自带的工具也可以使用navicat工具。

SQL语句-增删改查

  1. 插入数据

    insert into 表名(字段名1,字段名2) values(1,值2);
    

    例如:

    insert into user(name,description) values('陈浩南','铜锣湾扛把子');
    

    image-20200704162719944

  2. 删除数据

    delete from 表名 where 条件;
    

    条件一定要写,如果不写则会删除该表中所有的数据删除。

    例如:

    delete from user where id>3;
    
  3. 修改数据

    update 表名 set 字段名1=新值1,字段名2=新值2 where 条件;
    

    条件一定要写,如果不写则会修改数据表中的全部数据

    例如:

    update user set name='子风兄',description='比波波还骚' where id=3;
    
  4. 查询数据

    select * from 表名 [where 条件];
    

    image-20200704164131234

NodeJs操作数据库

NodeJs链接数据库需要使用模块mysql。基本结构如下:

var mysql = require("mysql");
// 创建一个链接数据库的链接
var connection = mysql.createConnection({
    // 数据库地址
    host: "localhost",
    // 数据库账号
    user: "root",
    // 数据库密码
    password: "root",
    // 数据库名(非表名)
    database: "study",
});
// 打开链接
connection.connect();

// 具体语句
// .....

connection.end();

连接与关闭链接可以不写。

connection.query("select * from user", (error, result, fields) => {
  // 如果查询遇到错误,则error代表错误。没有错误则为null
  console.log(error);
  // 执行sql语句得到的结果。如果查询遇到错误,则为undefined
  console.log(result);
  // 字段信息
  console.log(fields);
});

result

image-20200704170616466

fields

image-20200704170635370

let name = "伦哥";
let description = "这是个描述";
connection.query(
    `insert into user(name,description) values('${name}','${description}')`,
    (error, result, fields) => {
        // 如果查询遇到错误,则error代表错误。没有错误则为null
        console.log(error);
        // 执行sql语句得到的结果。如果查询遇到错误,则为undefined
        console.log(result);
        // 字段信息
        console.log(fields);
    }
);

其中result会返回一个对象,fields返回undefined。其中affectedRows表示受影响的行数,如果大于0则表示新增成功。

image-20200704171121849

let id = 3;
connection.query(`delete from user where id=${id}`, (error, result, fields) => {
    if (error == null) {
        console.log(result);
    }
});

image-20200704172128720

改与新增类似。

let name = "伦哥";
let description = "这是个描述";
let id = 3;
connection.query(
    `update user set name='${name}',description='${description}' where id=${id}`,
    (error, result, fields) => {
        if (error == null) {
            console.log(result);
        }
    }
);

image-20200704171859547

英雄管理系统-添加接口

app.post("/hero/add", upload.single("heroIcon"), (req, res) => {
  // 1.1 接收前端传递过来的参数
  console.log(req.file);
  console.log(req.body);
  let heroIcon = req.file.filename;
  let { heroName, heroSkill } = req.body;
  // 执行sql语句代码
  connection.query(
    `insert into hero(heroName,heroSkill,heroIcon) values('${heroName}','${heroSkill}','${heroIcon}');`,
    (error, result, fields) => {
      if (error == null) {
        res.send({
          code: 200,
          msg: "新增成功",
          list: { heroName: heroName, heroSkill: heroSkill },
        });
      } else {
        res.send({
          code: 400,
          msg: "新增失败",
          list: { heroName: heroName, heroSkill: heroSkill },
        });
      }
    }
  );
});

英雄管理系统-获取接口

app.get("/hero/all", (req, res) => {
    connection.query(
        `select id,heroName,heroSkill,heroIcon from hero where isDelete = 0`,
        (error, result, fields) => {
            if (error == null) {
                console.log(result);

                res.send({
                    code: 200,
                    msg: "查询成功",
                    list: result,
                });
            } else {
                res.send({
                    code: 400,
                    msg: "查询失败",
                    list: null,
                });
            }
        }
    );
});

完整代码

const express = require("express");
const multer = require("multer");
const mysql = require("mysql");

var connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "study",
});

const app = express();
var upload = multer({ dest: "uploads/" });
app.use(express.static("uploads"));
// 路由
// 参数:heroName heroSkill,heroIcon(文件),
app.post("/hero/add", upload.single("heroIcon"), (req, res) => {
    // 1.1 接收前端传递过来的参数
    console.log(req.file);
    console.log(req.body);
    let heroIcon = req.file.filename;
    let { heroName, heroSkill } = req.body;
    // 执行sql语句代码
    connection.query(
        `insert into hero(heroName,heroSkill,heroIcon) values('${heroName}','${heroSkill}','${heroIcon}');`,
        (error, result, fields) => {
            if (error == null) {
                res.send({
                    code: 200,
                    msg: "新增成功",
                    list: { heroName: heroName, heroSkill: heroSkill },
                });
            } else {
                res.send({
                    code: 400,
                    msg: "新增失败",
                    list: { heroName: heroName, heroSkill: heroSkill },
                });
            }
        }
    );
});

app.get("/hero/all", (req, res) => {
    connection.query(
        `select id,heroName,heroSkill,heroIcon from hero where isDelete = 0`,
        (error, result, fields) => {
            if (error == null) {
                console.log(result);

                res.send({
                    code: 200,
                    msg: "查询成功",
                    list: result,
                });
            } else {
                res.send({
                    code: 400,
                    msg: "查询失败",
                    list: null,
                });
            }
        }
    );
});

// 开启服务器
app.listen(3000, () => {
    console.log("开启成功");
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值