Node-知识点 – knex模块基本使用[重要]

释放双眼,带上耳机,听听看~!
node.js中为了简化SQL语句的编写,咱们可以使用knex这个模块来操纵数据库,方法简单的令人发指!

npm – knex

使用步骤:

1. 装包

//    cnpm i knex mysql --save

2. 用包

   const knex = require('knex')({
      client: 'mysql',
      connection: {
    //数据库地址
        host : '127.0.0.1',
   // 数据库用户名
        user : 'root',
  // 数据库密码
        password : 'root',
  // 数据库名
        database : 'student'
      }
    });

3. 执行增删改查的语句

// 3.1 查询数据
 // 语法: knex('表名').select().where(查询的条件,不写查询所有) .then(res=>{回调函数})
// 示例:
knex('student').select().where('id', '<', '4').then(res => {

    console.log(res);
})

// 3.2 新增数据
 // 语法: knex('表名').insert({需要新增的数据(对象)}) .then(res=>{回调函数})
// 示例:
knex('student').insert({ stu_name: '测试', stu_age: 18 }).then(res => {

    console.log(res);
})

// 3.3 删除数据
 // 语法: knex('表名').delete().where(删除的条件,不写删除所有数据) .then(res=>{回调函数})
// 示例:
knex('student').delete().where({ id: 3 }).then(res => {

    console.log(res);
})

// 3.4 修改数据
 /* 语法: knex('表名').update({需要修改的数据(对象)}).where(修改的条件,不写修改所有数据)
.then(res=>{回调函数})*/ 
// 示例:
knex('student').update({ stu_name: '嘻嘻', stu_age: 16 }).where({ id: 3 }).then(res => {

    console.log(res);
})
内容投诉
ES6JavaScript

JavaScript-ES6 - 箭头函数[常用到令人发指]

2020-8-22 18:05:26

Node

Node.js-跨域解决方案01 - CORS

2020-8-28 20:59:30

搜索