首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >七、NodeJs数据库管理

七、NodeJs数据库管理

作者头像
Dreamy.TZK
发布2020-07-07 18:30:36
1.9K0
发布2020-07-07 18:30:36
举报

建表

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

SQL语句-增删改查

  1. 插入数据 insert into 表名(字段名1,字段名2) values(值1,值2);例如: insert into user(name,description) values('陈浩南','铜锣湾扛把子');
image-20200704162719944
image-20200704162719944
  1. 删除数据 delete from 表名 where 条件;条件一定要写,如果不写则会删除该表中所有的数据删除。 例如: delete from user where id>3;
  2. 修改数据 update 表名 set 字段名1=新值1,字段名2=新值2 where 条件;条件一定要写,如果不写则会修改数据表中的全部数据 例如: update user set name='子风兄',description='比波波还骚' where id=3;
  3. 查询数据 select * from 表名 [where 条件];
image-20200704164131234
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
image-20200704170616466

fields

image-20200704170635370
image-20200704170635370

其他查询方法

  1. 以什么开头 select * from hero where heroName like "马%";
image-20200706192609590
image-20200706192609590
  1. 以什么结尾 select * from hero where heroName like "%韦";
image-20200706192724093
image-20200706192724093
  1. 包含什么内容 select * from hero where heroName like "%魔%";
image-20200706192800250
image-20200706192800250
  1. 并且条件 select * from hero where heroName like "%魔%" and isDelete='false';可用and链接多个条件。
image-20200706193125673
image-20200706193125673
  1. 或条件 select * from hero where heroName like "%魔%" or heroName like "%信%";
image-20200706193321722
image-20200706193321722
  1. 排序
    • 降序 select * from hero where heroName like "%魔%" or heroName like "%信%" order by id desc;
    image-20200706193441568
    image-20200706193441568
    • 升序 默认为升序 select * from hero where heroName like "%魔%" or heroName like "%信%" order by id asc;
    image-20200706193535578
    image-20200706193535578
    • 分页 倒序情况下拿到前20条数据 select * from hero order by id desc limit 0,20;
    image-20200706193830578
    image-20200706193830578
    • 连表查询 select 字段 from 表1 inner join 表2 on 对应关系select * from horder inner join customer on horder.cid = custom.id;可对两个表设置别名,但是后边也要设置别名。 select * from horder h inner join customer c on h.cid = c.id;也可以只查询某个字段。 select h.id,h.oname,c.price,c.id,c.cname,c.age from horder h inner join customer c on h.cid = c.id;

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
image-20200704171121849

let id = 3;
connection.query(`delete from user where id=${id}`, (error, result, fields) => {
    if (error == null) {
        console.log(result);
    }
});
image-20200704172128720
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
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("开启成功");
});
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 建表
  • SQL语句-增删改查
  • NodeJs操作数据库
      • 其他查询方法
            • 英雄管理系统-添加接口
            • 英雄管理系统-获取接口
            • 完整代码
            相关产品与服务
            数据库
            云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档