最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uni-app操作數(shù)據(jù)庫的三種方法總結(jié)

 更新時間:2023年05月23日 09:37:02   作者:lqj_本人  
數(shù)據(jù)庫操作的,可以采用多方案,下面這篇文章主要給大家介紹了關(guān)于uni-app操作數(shù)據(jù)庫的三種方法,文中通過實例代碼和圖文介紹的非常詳細,需要的朋友可以參考下

前端與后端(云端)分離實現(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)文章

最新評論

陈巴尔虎旗| 南靖县| 科尔| 七台河市| 新巴尔虎右旗| 明溪县| 澜沧| 眉山市| 巴林左旗| 镇康县| 镇江市| 沐川县| 杨浦区| 甘洛县| 江源县| 喀喇沁旗| 丰镇市| 邢台市| 通山县| 伊宁县| 平昌县| 高台县| 广水市| 万载县| 巴彦县| 临朐县| 浦北县| 惠水县| 佛教| 长子县| 永胜县| 浦城县| 福安市| 高邑县| 平遥县| 绥滨县| 汉中市| 墨竹工卡县| 清苑县| 图木舒克市| 牟定县|