To:海绵宝宝的朋友们 非完整版
目录标题
数据库创建
开通云开发后,可以看到这个界面。
在界面中可以创建新的表,数据。
注意:导入数据导入的是JSON文件。
管理数据库用CMS
创建数据表
也称创建内容模型
1.创建模型
2.在内容类型的地方设置表的字段
以下列举了两个类型(单字符+枚举)
3.在内容集合的表里进行数据的新建或导入
填入对应字段的信息
这样就创建好了数据
小程序与数据库连接
云开发环境的初始化
在小程序app.js文件里写入云开发的 环境ID
注意:环境ID在云开发的设置里。
在小程序中建立连接
在开发者文档里有描述,我们需要把这个代码写进界面的 .js文件里
从数据库读取信息的操作
注意:如果希望用户可以读到信息,要先对表的权限进行管理
查询操作
数据库增删改查
数据查询
查询所有数据
wx.cloud.database().collection('test').get()
.then(res => {
console.log('请求成功',res)
this.setData({
list: res.data
})
})
.catch(err =>{
console.log('请求失败',err)
})
查询符合条件的数据
用的是 .where方法
wx.cloud.database().collection('test')
.where({
txt:'Test01'
})
.get()
.then(res => {
console.log('请求成功',res)
})
.catch(err =>{
console.log('请求失败',err)
})
查询单条数据
用的是 .doc方法
wx.cloud.database().collection('test')
.doc('28ee4e3e6034809b071076086c7693c2')
.get()
.then(res => {
console.log('请求成功',res)
})
.catch(err =>{
console.log('请求失败',err)
})
数据增加
用的是 .add 方法
wx.cloud.database().collection('test')
.add({
data:{
meiju:'4',
txt:'Test04'
}
})
.then(res => {
console.log('请求成功',res)
})
.catch(err =>{
console.log('请求失败',err)
})
从表单中获取数据并进行增加操作
.wxml
<view wx:if="{{!isAdmin}}" class="container">
<view class='line_1px' />
<view class='item_root'>
账号:
<input class="textarea25px" bindinput="getName" value="{{username}}" maxlength='10' />
</view>
<view class='line_1px' />
<view class='item_root'>
密码:
<input class="textarea25px" bindinput="getPassWord" value="{{phone}}" maxlength='11' />
</view>
<!-- 提交按钮 -->
<button class='btn_fabu' bindtap='formSubmit' type='primary'>管理员登陆</button>
</view>
.js
//管理员登陆相关
getName: function (e) {
name = e.detail.value
},
getPassWord: function (e) {
password = e.detail.value
},
formSubmit: function () {
if (name == '' || name == undefined) {
wx.showToast({
title: '用户名不能为空',
icon: 'none'
})
return;
}
if (password == '' || password == undefined) {
wx.showToast({
title: '密码不能为空',
icon: 'none'
})
return;
}
this.login(name, password)
},
//登录
login(name, password) {
db.collection('admin').where({
name: name,
password: password
}).get()
.then(res => {
console.log("登陆成功", res)
if (res.data && res.data.length > 0) {
this.setData({
isAdmin: true
})
let admin = {
name: name,
password: password
}
wx.setStorageSync('admin', admin)
} else {
this.setData({
isAdmin: false
})
wx.showToast({
icon: 'none',
title: '账号或密码错误',
})
}
}).catch(res => {
console.log("登陆失败", res)
wx.showToast({
icon: 'none',
title: '账号或密码错误',
})
this.setData({
isAdmin: false
})
})
}
。。。。