关于sequelize的准备工作这里不再赘述.

一、引入sequelize模块

  1. var Sequelize = require('sequelize');

二、连接数据库

  1. var sequelize = new Sequelize(
  2. 'sample', // 数据库名
  3. 'root',   // 用户名
  4. 'psw',   // 用户密码
  5. {
  6. 'dialect': 'mysql',  // 数据库使用mysql
  7. 'host': 'localhost', // 数据库服务器ip
  8. 'port': 3306,        // 数据库服务器端口
  9. 'define': {
  10. // 字段以下划线(_)来分割(默认是驼峰命名风格)
  11. 'underscored': true
  12. }
  13. }
  14. );

三、定义表

  1. var User = sequelize.define(
  2. 'user',
  3. {
  4. userId: {
  5. field: 'user_id',
  6. primaryKey: true,
  7. type: Sequelize.BIGINT,
  8. allowNull: false
  9. },
  10. userName: {
  11. field: 'user_name',
  12. type: Sequelize.STRING,
  13. allowNull: false
  14. },
  15. userIcon: {
  16. field: 'user_icon',
  17. type: Sequelize.STRING,
  18. allowNull: true
  19. },
  20. title: {
  21. field: 'title',
  22. type: Sequelize.STRING,
  23. allowNull: true
  24. },
  25. gender: {
  26. field: 'gender',
  27. type: Sequelize.ENUM('MALE','FEMALE'),
  28. allowNull: true
  29. },
  30. birth: {
  31. field: 'birth',
  32. type: Sequelize.STRING,
  33. allowNull: true
  34. },
  35. mail: {
  36. field: 'mail',
  37. type: Sequelize.STRING,
  38. allowNull: true
  39. },
  40. tel: {
  41. field: 'tel',
  42. type: Sequelize.STRING,
  43. allowNull: true
  44. },
  45. mobile: {
  46. field: 'mobile',
  47. type: Sequelize.STRING,
  48. allowNull: true
  49. },
  50. updateTime: {
  51. field: 'update_time',
  52. type: Sequelize.STRING,
  53. allowNull: true
  54. }
  55. },
  56. {
  57. tableName: 'user',
  58. timestamps: false,
  59. freezeTableName: true
  60. }
  61. );

四、往表里添加数据

  1. User.create({
  2. userId: 23,
  3. userName: '老杨',
  4. updateTime: '2016-01-22 18:37:22'
  5. });

五、修改表内数据

  1. var pram={'userName':'晓博'};
  2. user.update(
  3. pram,{
  4. 'where':{'userId':{eq:23}}
  5. }
  6. );//将userId等于23的userName改为'晓博'

六、删除表内数据

    1. user.destroy({'where':{'id':{eq:23}}});//将表内userId等于23的元组删除

sequekize的更多相关文章

随机推荐

  1. Controller 的 Action 只接受 Ajax 请求

    ASP.NET MVC 使 Controller 的 Action 只接受 Ajax 请求. 2014-08-27 14:19 by h82258652, 555 阅读, 2 评论, 收藏, 编辑 首 ...

  2. 关于安装Redmine服务启动和邮件设置

    关于安装Redmine服务启动和邮件设置 分类: Redmine2009-06-01 10:37 5658人阅读 评论(0) 收藏 举报 authentication邮件服务器serviceexcha ...

  3. 如何在局域网安装Redmine(转贴)

    如何在局域网安装Redmine(转贴) 分类: Redmine2009-06-01 10:31 1740人阅读 评论(0) 收藏 举报 phpmyadmin项目管理railssubversion数据库 ...

  4. 根据首尾字节的tcp分包断包算法

    这个算是我的一点小总结吧,放出来分享给大家,原来在网上找这种算法都找了N久没找到,自己写也是走了许多弯路,就放出来遛一遛吧 大家将就这个看看, 这是其中的一个主要的方法,其余的我就不放出来了,其中的I ...

  5. Introducing ASP.NET vNext and MVC 6

    [译]Introducing ASP.NET vNext and MVC 6 原文:http://www.infoq.com/news/2014/05/ASP.NET-vNext?utm_source ...

  6. Java中的嵌套类和内部类

    以前看<Java编程思想>的时候,看到过嵌套类跟内部类的区别,不过后来就把它们的概念给忘了吧.昨天在看<数据结构与算法分析(Java语言版)>的时候,又遇到了这个概念,当时就很 ...

  7. Enumerable和yield

    说说IEnumerable和yield IEnumerable数据类型是我比较喜欢的数据类型,特别是其强类型IEnumerable<T>更获得Linq的支持使得代码看起来更加优雅.整洁. ...

  8. spring mvc在普通类中获取HttpServletRequest对象

    如题,需要在web.xml中配置request监听,如下 <listener> <description>spring request监听器</description&g ...

  9. DirectX And Com

    Windows游戏编程读书笔记(5)——初识DirectX和COM 一.COM 1.什么是COM对象 一个COM对象事实上是一个或一套实现了大量接口的C++类 2.COM的优点 不用重新编译你的程序就 ...

  10. Topics

    Topics Introduction (starting with old devices) How to handle a new Firmware How to set up your Mac ...