uni-app云数据库常用操作

数据库查询

const db = uniCloud.database()
// 以user表为例
db.collection('user').get().then((res)=>{
    // 数据对象数组res.result.data
}).catch((err)=>{
    console.log(err.code)
	console.log(err.message)
})

// 加上where
db.collection('user').where({name: '...'}).get().then((res)=>{
    // 数据操作
    // 如果只有一个数据,那么为res.result.data[0]
}).catch((err)=>{
    console.log(err.code)
	console.log(err.message)
})

这里面的this是可以指Vue实例的,所以是可以直接访问和修改data数据

数据库添加

const db = uniCloud.database()
db.collection('user').add({
    name: this.username,
    password: this.password,
}).then((res)=>{
    // 数据操作
}).catch((err)=>{
    console.log(err.code)
	console.log(err.message)
})

数据库删除

const db = uniCloud.database()
db.collection('user').where({
    name: username
}).remove().then((res)=>{
    uni.showToast({
        title: '用户删除成功',
        icon: 'success'
    })
}).catch((err)=>{
    console.log(err.code)
	console.log(err.message)
})

数据库更新

const db = uniCloud.database()
db.collection('user').where({
    name: username
}).update({
    age: 20
}).then((res)=>{
    if (res > 0) {
        // 数据更新成功
    }
}).catch((err)=>{
    console.log(err.code)
	console.log(err.message)
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值