sequekize
关于sequelize的准备工作这里不再赘述.
一、引入sequelize模块
- var Sequelize = require('sequelize');
二、连接数据库
- var sequelize = new Sequelize(
- 'sample', // 数据库名
- 'root', // 用户名
- 'psw', // 用户密码
- {
- 'dialect': 'mysql', // 数据库使用mysql
- 'host': 'localhost', // 数据库服务器ip
- 'port': 3306, // 数据库服务器端口
- 'define': {
- // 字段以下划线(_)来分割(默认是驼峰命名风格)
- 'underscored': true
- }
- }
- );
三、定义表
- var User = sequelize.define(
- 'user',
- {
- userId: {
- field: 'user_id',
- primaryKey: true,
- type: Sequelize.BIGINT,
- allowNull: false
- },
- userName: {
- field: 'user_name',
- type: Sequelize.STRING,
- allowNull: false
- },
- userIcon: {
- field: 'user_icon',
- type: Sequelize.STRING,
- allowNull: true
- },
- title: {
- field: 'title',
- type: Sequelize.STRING,
- allowNull: true
- },
- gender: {
- field: 'gender',
- type: Sequelize.ENUM('MALE','FEMALE'),
- allowNull: true
- },
- birth: {
- field: 'birth',
- type: Sequelize.STRING,
- allowNull: true
- },
- mail: {
- field: 'mail',
- type: Sequelize.STRING,
- allowNull: true
- },
- tel: {
- field: 'tel',
- type: Sequelize.STRING,
- allowNull: true
- },
- mobile: {
- field: 'mobile',
- type: Sequelize.STRING,
- allowNull: true
- },
- updateTime: {
- field: 'update_time',
- type: Sequelize.STRING,
- allowNull: true
- }
- },
- {
- tableName: 'user',
- timestamps: false,
- freezeTableName: true
- }
- );
四、往表里添加数据
- User.create({
- userId: 23,
- userName: '老杨',
- updateTime: '2016-01-22 18:37:22'
- });
五、修改表内数据
- var pram={'userName':'晓博'};
- user.update(
- pram,{
- 'where':{'userId':{eq:23}}
- }
- );//将userId等于23的userName改为'晓博'
六、删除表内数据
- user.destroy({'where':{'id':{eq:23}}});//将表内userId等于23的元组删除
sequekize的更多相关文章
随机推荐
- Controller 的 Action 只接受 Ajax 请求
ASP.NET MVC 使 Controller 的 Action 只接受 Ajax 请求. 2014-08-27 14:19 by h82258652, 555 阅读, 2 评论, 收藏, 编辑 首 ...
- 关于安装Redmine服务启动和邮件设置
关于安装Redmine服务启动和邮件设置 分类: Redmine2009-06-01 10:37 5658人阅读 评论(0) 收藏 举报 authentication邮件服务器serviceexcha ...
- 如何在局域网安装Redmine(转贴)
如何在局域网安装Redmine(转贴) 分类: Redmine2009-06-01 10:31 1740人阅读 评论(0) 收藏 举报 phpmyadmin项目管理railssubversion数据库 ...
- 根据首尾字节的tcp分包断包算法
这个算是我的一点小总结吧,放出来分享给大家,原来在网上找这种算法都找了N久没找到,自己写也是走了许多弯路,就放出来遛一遛吧 大家将就这个看看, 这是其中的一个主要的方法,其余的我就不放出来了,其中的I ...
- 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 ...
- Java中的嵌套类和内部类
以前看<Java编程思想>的时候,看到过嵌套类跟内部类的区别,不过后来就把它们的概念给忘了吧.昨天在看<数据结构与算法分析(Java语言版)>的时候,又遇到了这个概念,当时就很 ...
- Enumerable和yield
说说IEnumerable和yield IEnumerable数据类型是我比较喜欢的数据类型,特别是其强类型IEnumerable<T>更获得Linq的支持使得代码看起来更加优雅.整洁. ...
- spring mvc在普通类中获取HttpServletRequest对象
如题,需要在web.xml中配置request监听,如下 <listener> <description>spring request监听器</description&g ...
- DirectX And Com
Windows游戏编程读书笔记(5)——初识DirectX和COM 一.COM 1.什么是COM对象 一个COM对象事实上是一个或一套实现了大量接口的C++类 2.COM的优点 不用重新编译你的程序就 ...
- Topics
Topics Introduction (starting with old devices) How to handle a new Firmware How to set up your Mac ...