uni-app操作數(shù)據(jù)庫的三種方法總結(jié)
前端與后端(云端)分離實現(xiàn)數(shù)據(jù)庫的操作
1.使用云函數(shù)來操作數(shù)據(jù)庫
第一步:
創(chuàng)建一個云函數(shù)并部署
第二部:
在云函數(shù)中寫入數(shù)據(jù)庫的操作代碼:
const db = uniCloud.database();
exports.main = async (event, context) => {
//event為客戶端上傳的參數(shù)
//返回數(shù)據(jù)給客戶端
return await db.collection("News_system").add({
xingming:"張三"
})
};第三步:
前端代碼:
<template>
<view>
<button @click="add">添加</button>
</view>
</template>
<script>
// const db = uniCloud.database();
export default {
data() {
return {
}
},
onLoad() {
},
methods: {
add(){
// return db.collection("News_system").add({
// xingming:"1111"
// }).then(res=>{
// console.log(res)
// })
uniCloud.callFunction({
name:"hanshu1"
}).then(res=>{
console.log(res)
})
}
}
}
</script>
<style>
</style>顯示數(shù)據(jù)庫數(shù)據(jù)表信息添加情況:

2.使用云對象來操作數(shù)據(jù)庫
第一步:
先創(chuàng)建一個云對象并寫入相應(yīng)的數(shù)據(jù)庫操作的代碼:
const db = uniCloud.database();
module.exports = {
_before: function () { // 通用預(yù)處理器
},
add:()=>{
db.collection("News_system").add({
nianling:16
})
}
}第二步:
將其上傳部署!
第三步前端代碼:
<template>
<view>
<button @click="add">添加</button>
</view>
</template>
<script>
const duixiang1 = uniCloud.importObject("duixiang1")
// const db = uniCloud.database();
export default {
data() {
return {
}
},
onLoad() {
},
methods: {
add(){
duixiang1.add().then(res=>{
console.log(res)
})
// return db.collection("News_system").add({
// xingming:"1111"
// }).then(res=>{
// console.log(res)
// })
// uniCloud.callFunction({
// name:"hanshu1"
// }).then(res=>{
// console.log(res)
// })
}
}
}
</script>
<style>
</style>注意:我這里必選先調(diào)用一下我們寫的云對象,然后調(diào)用后直接對我們新賦值的參數(shù)進行操作即可!
const duixiang1 = uniCloud.importObject("duixiang1")duixiang1.add().then(res=>{
console.log(res)回到數(shù)據(jù)庫:我們可以發(fā)現(xiàn)我們點擊按鈕時,我們想添加的數(shù)據(jù)已經(jīng)在我們的數(shù)據(jù)庫中了!

前端與后端(云端)結(jié)合實現(xiàn)客戶端操作數(shù)據(jù)庫
3.使用DB Schema結(jié)構(gòu)規(guī)范實現(xiàn)客戶端對數(shù)據(jù)庫的操作
第一步:

我們線在數(shù)據(jù)庫中創(chuàng)建一個數(shù)據(jù)表。
第二步:


在 uniCloud中的database中右鍵,后選擇下載所有DB Schema及擴展校驗函數(shù)。
等在下載完畢后我們會發(fā)現(xiàn)

我們的數(shù)據(jù)表:News_system已經(jīng)在database中。
第三步:
然后我們打開它后,我們將原來的表文件改為:

注意:
將:
"read": true, "create": true,
是將我們數(shù)據(jù)表中的權(quán)限的”讀取“和”添加“打開!
properties中我們需要添加我們結(jié)合前端的需要在數(shù)據(jù)表中添加的數(shù)據(jù)(請結(jié)合第四步理解):
"xingming": {
"bsonType": "string",
"title": "姓名"
}第四步:
<template>
<view>
<button @click="add">添加</button>
</view>
</template>
<script>
const db = uniCloud.database();
export default {
data() {
return {
}
},
onLoad() {
},
methods: {
add(){
return db.collection("News_system").add({
xingming:"1111"
}).then(res=>{
console.log(res)
})
}
}
}
</script>
<style>
</style>我們直接在前端對數(shù)據(jù)庫進行一系列的操作:
const db = uniCloud.database();
return db.collection("News_system").add({
xingming:"1111"
}).then(res=>{
console.log(res)
})我們這里想要添加的
xingming:"1111"
要必須在第三步中的數(shù)據(jù)表中進行xingming這個參數(shù)名的配置。
如第三步中的:
"xingming": {
"bsonType": "string",
"title": "姓名"
}回到前端頁面:

我們發(fā)現(xiàn),當我們點擊添加時,后臺已經(jīng)給我們打印出來了東西,說明我們的數(shù)據(jù)表的權(quán)限已經(jīng)處于打開狀態(tài),并且此時我們的想要添加的數(shù)據(jù)已經(jīng)成功的添加到數(shù)據(jù)庫中。
回到后端(云端)數(shù)據(jù)庫:

我們可以發(fā)現(xiàn),我們通過修改數(shù)據(jù)表權(quán)限的方式能實現(xiàn)通過純前端的操作就可以實現(xiàn)對數(shù)據(jù)庫進行操作!
總結(jié)
到此這篇關(guān)于uni-app操作數(shù)據(jù)庫的三種方法的文章就介紹到這了,更多相關(guān)uni-app操作數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript封裝addLoadEvent實現(xiàn)頁面同時加載執(zhí)行多個函數(shù)的方法
這篇文章主要介紹了javascript封裝addLoadEvent實現(xiàn)頁面同時加載執(zhí)行多個函數(shù)的方法,實例分析了onload事件執(zhí)行的原理與同時執(zhí)行多個函數(shù)功能的實現(xiàn)技巧,需要的朋友可以參考下2016-07-07
js中document.write和document.writeln的區(qū)別
這篇文章主要介紹了js中document.write和document.writeln的區(qū)別,需要的朋友可以參考下2018-03-03
微信小程序使用ucharts在小程序中加入橫屏展示功能的全過程
這篇文章主要給大家介紹了關(guān)于微信小程序使用ucharts在小程序中加入橫屏展示功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用微信小程序具有一定的參考學習價值,需要的朋友可以參考下2022-09-09
Javascript中Microtask和Macrotask鮮為人知的知識點
這篇文章主要為大家介紹了Javascript中Microtask和Macrotask鮮為人知的知識點講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04
web前端開發(fā)中常見的多列布局解決方案整理(一定要看)
多列布局在web前端開發(fā)中也是較為常見的,今天小編給大家介紹這里會提到的多列布局有兩列定寬加一列自適應(yīng)、多列不定寬加一列自適應(yīng)、多列等分三種,感興趣的朋友一起看看吧2017-10-10

