4.7【微信小程序全栈开发课程】意见反馈(三)--操作数据库knex

这一节我们要学一个新的知识点,在数据库中创建opinion数据表,然后在意见反馈页面,点击提交按钮,将填写的反馈数据提交到opinion数据表中

1、数据表的字段

字段名 字段说明 字段类型 备注
openid 用户微信标识 string 必填
opinion 用户反馈信息 text 必填
src 图片链接 text 选填
wechat 微信号 string 选填
create_time 创建时间 timestamp 必填

2、创建opinions数据表

(1)登录数据库
~/WeChatProjects/truth_hold$ mysql -uroot -p
(2)登录成功之后,选择cAuth表
mysql> use cAuth;
(3)在数据库中创建opinions表,粘贴下面数据直接回车即可
DROP TABLE IF EXISTS `opinions`;

CREATE TABLE `opinions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `openid` varchar(100) NOT NULL,
  `opinion` text NOT NULL,
  `src` text NOT NULL,
  `wechat` varchar(20) DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
(4)查看当前数据库中的表,已经有opinions数据表了
mysql> show tables;
+-----------------+
| Tables_in_cauth |
+-----------------+
| cSessionInfo    |
| opinions        |
+-----------------+
2 rows in set (0.00 sec)

3、操作数据表

微信小程序中用的是knex来操作数据库,我们先来总结下knex增删改查与mysql增删改查之间的区别,大体了解一下就可以了,后面我们会在项目中实践

查询
mysql('opinions')
.where({openid:'oQPCO4ol5YCO4ol5Y',wechat:'maoningyi1'})
.select()

//对应的SQL语句
select * from opinions where openid='oQPCO4ol5YCO4ol5Y' and wechat='maoningyi1';
新增
mysql('opinions')
.insert({openid:'oQPCO4ol5YCO4ol5Y',wechat:'maoningyi1'})

//对应的SQL语句
insert into opinions ("openid","wechat") values ("oQPCO4ol5YCO4ol5Y",'maoningyi1');
修改
mysql('opinions')
.where('openid','oQPCO4ol5YCO4ol5Y')
.update({
    wechat:'maoningyi1'
})

//对应的SQL语句
update opinions set wechat='maoningyi1' where openid='oQPCO4ol5YCO4ol5Y';
删除
mysql('opinions')
.where('openid','oQPCO4ol5YCO4ol5Y')
del()

//对应的SQL语句
delete from opinions where openid='oQPCO4ol5YCO4ol5Y';

作者:猫宁一
95后全栈程序媛₍ᐢ •⌄• ᐢ₎一枚~ 热爱学习!热爱编程!
可关注【猫宁一】公众号领取我所有全栈项目代码哦~

点击查看课程目录:微信小程序全栈开发课程目录

发布了131 篇原创文章 · 获赞 136 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/shine_a/article/details/102610987
今日推荐