微信小程序云數(shù)據(jù)庫,可以幫助我們快速構(gòu)建小程序,讓我們更輕松地實(shí)現(xiàn)數(shù)據(jù)庫的功能,同時(shí)又不必花費(fèi)大量時(shí)間去編寫復(fù)雜的數(shù)據(jù)庫代碼。這篇文章將介紹如何使用微信小程序云數(shù)據(jù)庫來實(shí)現(xiàn)一些常見的數(shù)據(jù)庫功能。
?
創(chuàng)建云數(shù)據(jù)庫?
首先,我們需要?jiǎng)?chuàng)建一個(gè)云數(shù)據(jù)庫,在小程序的開發(fā)工具中,點(diǎn)擊“騰訊云”按鈕,在騰訊云控制臺中創(chuàng)建一個(gè)數(shù)據(jù)庫,如下圖所示:?
創(chuàng)建集合?
接下來,我們需要?jiǎng)?chuàng)建集合,也就是我們常說的表,在云數(shù)據(jù)庫中,我們可以創(chuàng)建多個(gè)集合,每個(gè)集合中可以包含多個(gè)字段,如下圖所示:
??
初始化數(shù)據(jù)庫對象
const db = wx.cloud.database()
添加數(shù)據(jù)?
小程序云數(shù)據(jù)庫添加數(shù)據(jù),代碼如下:
db.collection("web0308")
.add({
data:{
id: 3,
name: "囂張哥",
age: 22,
sex: "男",
sal: 6780
}}
)
.then(res=>{
console.log(res)
})
.catch(error=>{ console.log(error)})
?通過上面的代碼,我們可以很輕松地添加數(shù)據(jù)到我們創(chuàng)建的云數(shù)據(jù)庫中
.collection()這個(gè)參數(shù),要填寫集合的名字?
查詢數(shù)據(jù)?
小程序云數(shù)據(jù)庫查詢數(shù)據(jù),代碼如下:?
db.collection("web0308")
// .where({
//name: "張飛"
//age: db.command.lt(23)
//age: db.command.gt(22)
// })
.get()
.then(res=>{
console.log(res)
})
.catch(error=>{ console.log(error)})
?.where 可以添加過濾條件,如果不寫返回的就是全部
更新數(shù)據(jù)
?小程序云數(shù)據(jù)庫更新數(shù)據(jù),代碼如下:
db.collection("web0308")
.doc("30d33d4264083a5a001412d82546ad60")
.update({
data:{
name: "張飛"
}
})
.then(res=>{ console.log(res)})
.catch(error=>{ console.log(error)})
?doc這個(gè)條件要填寫數(shù)據(jù)的_id, 這個(gè)_id在添加數(shù)據(jù)的時(shí)候系統(tǒng)自動(dòng)生成, 唯一的標(biāo)識
刪除數(shù)據(jù)
?小程序云數(shù)據(jù)庫刪除數(shù)據(jù),代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-451322.html
db.collection("web0308")
.doc("30d33d4264083a5a001412d82546ad60")
.remove()
.then(res=>{ console.log(res)})
.catch(error=>{ console.log(error.errMsg)})
刪除數(shù)據(jù)時(shí),也要.doc這個(gè)條件,找到具體數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-451322.html
到了這里,關(guān)于[微信小程序 開發(fā)] 云數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!