mysql2 installation and use

mysql2 module in nodejs

installation

npm i mysql2 -S

use

const mysql2 = require("mysql2");
配置和数据库之间的连接
const connection = mysql2.createConnection({
    
    
    host: 'localhost',  //主机
    user: "root", //用户名
    password: "102327", //自己设置的密码
    database: "js05", //表名
    charset: "utf8"
});

app.use(static(__dirname + "/static"));
// app.use(views(__dirname + "/views"));
router.get("/", async ctx => {
    
    
    let mydata;
    
    let sql = "SELECT age,hobby FROM user WHERE id<=2";//引用数据库中的数据操作 curd操作
    
    // connection.query(sql, (err, result) => {
    
    
    //     if (err) {
    
    
    //         console.log(err);
    //     }
    //     console.log(result);
    //     mydata = result;
    // })
    // console.log(mydata);

    try {
    
    
    //解构赋值   块级作用域
        let [rows] = await connection.promise().query(sql);
        mydata = rows;
    } catch (err) {
    
    
        console.log(err);
    }
    //数组
    ctx.body = mydata; //页面显示
    console.log(mydata)//数组[ TextRow { age: 20, hobby: '篮球' }, TextRow { age: 15, hobby: '学医' } ]
    
        // ctx.body = "hello"
})
app.use(router.routes());
app.listen(8844);

Combination of vsc and graphics

  • Bubble sort fast sort binary search
添加内容  图形化
    年龄小于20 且姓王的数据
    let sql = "SELECT * FROM user WHERE age<20 AND username LIKE '佳佳'";

    年龄的按序排列的数据   ORDER  BY  条件 ASC正序(省略)  DESC倒叙
    let sql = "SELECT * FROM user WHERE age >15 ORDER BY age ASC"; //正序

    限制查询  LIMIT   //分页
    let sql = "SELECT * FROM user LIMIT 2,2"

    别名 : AS
    连表查询    附表  LEFT JOIN  主表
                ON:  表之间的数据连接
别名 : AS
    let sql = "SELECT u.username,u.age FROM user AS u "


    连表查询    附表  LEFT JOIN  主表
                ON:  表之间的数据连接
    let sql = "SELECT u.username,p.pname FROM  products AS p LEFT JOIN user AS u ON p.uid=u.id "

Add json data to the graph with node

async function inserData() {
    
    
    for (let i = 0; i < newsData.length; i++) {
    
    
        let title = newsData[i].title;
        let content = newsData[i].content;
        let addTime = newsData[i].addTime;
        let country = newsData[i].type;
        let type = newsData[i].type;
        let imgUrl = newsData[i].imgUrl;

        let sql = "INSERT INTO news (title,content,addTime,country,type,imgUrl) VALUES (?,?,?,?,?,?)";
        await connection.promise().query(sql, [title, content, addTime, country, type, imgUrl]);
    }
}

Insert picture description here

Connection between configuration and database

const connection = mysql2.createConnection({
    
    
    host: 'localhost',
    user: "root", //名
    password: "102327", //自己设置的密码
    database: "js05", //表名
    charset: "utf8"
});

News list data display

  • Installation requirements
const KOa = require("koa");
const Router = require("koa-router");
const static = require("koa-static");
const Views = require("koa-views");
const mysql2 = require("mysql2")

Add another npm i pug

    //配置和数据库之间的连接
const connection = mysql2.createConnection({
    
    
    host: 'localhost',
    user: "root", //名
    password: "102327", //自己设置的密码
    database: "js05", //表名
    charset: "utf8"
});

app.use(static(__dirname + "/static"));
app.use(Views(__dirname + "/views", {
    
    
    extension: "pug"
}));

router.get("/", ctx => {
    
    
    // ctx.body = "hello";
    ctx.redirect("/index");
})
router.get("/index", async ctx => {
    
    
    let perPage = 5;
    let p = parseInt(ctx.query.p) || 1;

    // let newData = JSON.parse(JSON.stringify(data)).splice((p - 1) * perPage, p);
    //获取对饮的数据
    let [data] = await connection.promise().query("SELECT * FROM news")
    let [newData] = await connection.promise().query("SELECT * FROM news LIMIT ?,?", [(p - 1) * perPage, perPage]) //限制查询  LIMIT
    let totalPage = Math.ceil(data.length / perPage);
    await ctx.render("index", {
    
    
        newData,
        totalPage,
        data,
        p
    })
})

Guess you like

Origin blog.csdn.net/weixin_54645137/article/details/115294546