一分钟教你学会小程序云开发的数据库的增删改查

一分钟教你学会小程序云开发的数据库的增删改查

1. 数据库的查询

onLoad(){
    
    
  wx.cloud.database().collection('lost')
  .get()
  .then(res=>{
    
    
  console.log('成功',res)
    this.setData({
    
    
      list:res.data
    })
  })
  .catch(res=>{
    
    
    console.log('失败',res)
  })
},

2.数据库的添加

注意:在填数据中添加数据时必须保证,data中的数据名称必须和数据库中的一致。
在这里插入图片描述

onLoad(){
    
    
 wx.cloud.database().collection('express')
    .add({
    
    
      data:{
    
    
      message:message,
      place:place,
      number:number,
      money:money,
      more:more,
      }
    })
    .then(res=>{
    
    
      console.log('添加成功',res)
    })
    .catch(res=>{
    
    
      console.log('提交失败',res)
      })
    }

3.数据库中数据的删除

注意:数据库中数据的删除是通过这条数据的id来进行操作的,在删除数据时要进行删除数据id的获取,然后通过数据的id来对数据进行操作。数据id的获取是通过在button按钮中添加来实现data-id=’{ {item._id}}'的获取。

onLoad(e){
    
    
        var that=this;
        let id = e.currentTarget.dataset.id
        wx.cloud.database().collection('electric').doc(id).remove({
    
    
          success:res=>{
    
    
            wx.showToast({
    
    
              icon:'none',
              title: '删除成功',
            })
          }
       fail:res=>{
    
    
        wx.showToast({
    
    
              icon:'none',
              title: '删除成功',
            })
      }
  })
},

4.数据库数据的修改

修改和删除类似,也是通过获取数据的id进行的操作

onLoad(e){
    
    
        var that=this;
        let id = e.currentTarget.dataset.id
        wx.cloud.database().collection('electric').doc(id).updata({
    
    
           data:{
    
    
           age:age
           },
          success:res=>{
    
    
            wx.showToast({
    
    
              icon:'none',
              title: '修改成功',
            })
          }
       fail:res=>{
    
    
         wx.showToast({
    
    
              icon:'none',
              title: '失败成功',
            })
      }
  })
},

大家可以关注我的公众号,里面有更多的小程序项目

在这里插入图片描述

おすすめ

転載: blog.csdn.net/qq_48164590/article/details/118938806