时间:2021-07-01 10:21:17 帮助过:23人阅读
- User.create({
- name: ‘XiaoMing‘,
- password: ‘1234567890‘,
- mail: ‘xiaoming@qq.com‘
- }).then(function(result){
- console.log(‘inserted XiaoMing ok‘);
- }).catch(function(err){
- console.log(‘inserted XiaoMing error‘);
- console.log(err.message);
- });
查询记录
调用模型对象的findAll方法进行查询操作,在参数中可以制定where条件。
where条件甚至可以支持数据库自身特有的函数。
where具体写法,参考:http://sequelize.readthedocs.io/en/latest/docs/querying/
- User.findAll({
- where:{
- name:{
- $like:‘Zhang%‘
- }
- }
- }).then(function(result){
- console.log(‘query all users‘);
- for (var i = 0, usr; usr = result[i++];) {
- console.log(‘nae=‘ + usr.name + ‘, password=‘ + usr.password + ‘, mail=‘ + usr.mail);
- }
- });
修改记录
调用模型对象的update方法进行更新操作,在第一个参数中指定更新的字段和值,在第二个参数中指定条件。
- User.update({
- password:‘12‘
- },{
- where:{
- name:{
- $like:‘Xiao%‘
- }
- }
- }).then(function(result){
- console.log(‘updated user‘);
- console.log(result);
- });
删除记录
调用模型对象的destroy方法进行删除操作,在参数中指定删除条件。
- User.destroy({
- where:{
- name:{
- $like:‘Zhang%‘
- }
- }
- }).then(function(result){
- console.log(‘destroy user‘);
- console.log(result);
- });
官方文档:戳
[转]在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查
标签:dial upd lan 数据 read 增删改 技术 查询 开源