uniCloud-用schema创建数据库表并插入记录
在当前项目的uniCloud-database目录上右键 "新建DB Schema" 填上要新建的表名字 table1.schema.json
在properties字段里 _id字段后面再追加字段
table1.schema.json
// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema
{
"bsonType": "object",
"required": ["title","content"], //添加记录时的必填字段
"permission": {
"read": true,
"create": true,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"title":{ // 字段名
"bsonType": "string",
"title":"文章标题",
"description": "文章标题说明",
"errorMessage":"标题是必填项" // 字段未填就会报这个错
},
"content":{ //字段名
"bsonType": "string",
"title": "内容",
"description": "文章内容"
},
"posttime":{ //字段名
"bsonType": "timestamp",
"title": "发布时间",
"description": "文章内容",
"defaultValue":{ //默认值,用服务器当前时间戳
"$env": "now"
}
},
"hits":{ //字段名
"bsonType": "int",
"title": "阅读量",
"defaultValue":33 //默认点击量33
}
}
}
index.vue
<template>
<view class="content">
<button @click="createTable" type="primary">新增记录</button>
</view>
</template>
<script>
// DB schema
const db = uniCloud.database()
export default {
methods: {
createTable() {
db.collection("table1").add({
title: "如何长胖10斤",
content: "好好吃饭,按时休息,好好工作.",
hits:10
}).then(res => {
console.log(res);
}, err => {
console.log(err.message);
})
}
}
}
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义