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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| const express = require("express") const app = express() const mysql2 = require("mysql2")
app.get("/", async (req, res) => { const config = getDBConfig() const promisePool = mysql2.createPool(config).promise() var gender = 1 var score = 60 var result = await promisePool.query("select * from students where gender=? and score>=?", [gender, score]) res.send({ ok: 1, data: result[0] }) })
app.get("/insert", async (req, res) => { const config = getDBConfig() const promisePool = mysql2.createPool(config).promise() var name = "狗蛋" var gender = 1 var score = 88 var class_id = 2 var result = await promisePool.query("insert into students(name, score, gender, class_id) values (?,?,?,?)", [name, score, gender, class_id]) res.send({ ok: 1, data: result[0] }) })
app.get("/update", async (req, res) => { const config = getDBConfig() const promisePool = mysql2.createPool(config).promise() var id = 8 var name = "狗剩" var score = 98 var result = await promisePool.query("update students set name=?, score=? where id=?", [name, score, id]) res.send({ ok: 1, data: result[0] }) })
app.get("/delete", async (req, res) => { const config = getDBConfig() const promisePool = mysql2.createPool(config).promise() var id = 3 var result = await promisePool.query("delete from students where id=?", [id]) res.send({ ok: 1, data: result[0] }) })
app.listen(3000)
function getDBConfig() { return { host: '127.0.0.1', prot: 3306, user: 'root', password: '123456', database: 'jerry_test', connectionLimit: 1 } }
|