node+koa2+mysql簡單入門增刪改查

一、連接數據庫,展示表中數據

這裏用的是koa2,來鏈接mysql,下面是建的簡單的表

 代碼:

const mysqlConfig = mysql.createPool({
    user: 'liulibin',//用戶
    password: '',//密碼
    database: '',//數據庫
    host: '',//數據庫地址
    port: ''
  })
  // 數據池中進行會話操作
  mysqlConfig.getConnection(function (err, connection) {
    connection.query('SELECT*FROM list', (err, results, fields) => {
      if (err) {
        throw err
      }
      console.log("result", results)//表中數據
      connection.release()//會話結束
    })
  })

 展示結果:

二、 添加數據

// 添加數據
router.get('/insert', async (ctx, next) => {
  // 創建數據池
  const mysqlConfig = mysql.createPool({
    user: 'liulibin',//用戶
    password: '',//密碼
    database: '',//數據庫
    host: '',//數據庫地址
    port: '3306'
  })
  // 數據池中進行會話操作
  mysqlConfig.getConnection(function (err, connection) {
    const addSql = 'INSERT INTO list(id,name) VALUES(0,?)';
    const addSqlData = ['趙四']
    connection.query(addSql, addSqlData, (err, results, fields) => {
      if (err) {
        throw err
      }
      console.log("result", results)//表中數據
      connection.release()//會話結束
    })
  })

  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

展示:

三、刪除數據

// 刪除數據
router.get('/delete', async (ctx, next) => {
  // 創建數據池
  const mysqlConfig = mysql.createPool({
    user: 'liulibin',//用戶
    password: '',//密碼
    database: '',//數據庫
    host: '',//數據庫地址
    port: '3306'
  })
  // 數據池中進行會話操作
  mysqlConfig.getConnection(function (err, connection) {
    const delSql = 'DELETE FROM list where id = 0';
    connection.query(delSql, (err, results, fields) => {
      if (err) {
        throw err
      }
      console.log("result", results)//表中數據
      connection.release()//會話結束
    })
  })

  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

展示:

四、改數據

// 改數據
router.get('/mod', async (ctx, next) => {
  // 創建數據池
  const mysqlConfig = mysql.createPool({
    user: 'liulibin',//用戶
    password: '',//密碼
    database: '',//數據庫
    host: '',//數據庫地址
    port: '3306'
  })
  // 數據池中進行會話操作
  mysqlConfig.getConnection(function (err, connection) {
    const modSql = 'UPDATE list SET name=? WHERE id=?';
    const modSqlData = ['小龍',1]
    connection.query(modSql, modSqlData, (err, results, fields) => {
      if (err) {
        throw err
      }
      console.log("result", results)//表中數據
      connection.release()//會話結束
    })
  })

  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})
// 改數據
router.get('/mod', async (ctx, next) => {
  // 創建數據池
  const mysqlConfig = mysql.createPool({
    user: 'liulibin',//用戶
    password: '',//密碼
    database: '',//數據庫
    host: '',//數據庫地址
    port: '3306'
  })
  // 數據池中進行會話操作
  mysqlConfig.getConnection(function (err, connection) {
    const modSql = "UPDATE list SET name='小王' WHERE id=1";
    // const modSqlData = "['小王',1]"
    connection.query(modSql, (err, results, fields) => {
      if (err) {
        throw err
      }
      console.log("result", results)//表中數據
      connection.release()//會話結束
    })
  })

  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

展示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章