zoukankan      html  css  js  c++  java
  • sequekize

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

    一、引入sequelize模块

    [javascript] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. var Sequelize = require('sequelize');  


    二、连接数据库

    [javascript] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    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. );  


    三、定义表

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

    四、往表里添加数据

    [javascript] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. User.create({  
    2.     userId: 23,  
    3.     userName: '老杨',  
    4.     updateTime: '2016-01-22 18:37:22'  
    5. });  

    五、修改表内数据

    [javascript] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. var pram={'userName':'晓博'};  
    2.   
    3. user.update(  
    4.   
    5.     pram,{  
    6.           
    7.             'where':{'userId':{eq:23}}  
    8. }  
    9. );//将userId等于23的userName改为'晓博'  

    六、删除表内数据

    [javascript] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
      1. user.destroy({'where':{'id':{eq:23}}});//将表内userId等于23的元组删除  
  • 相关阅读:
    iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
    iOS NSArray数组过滤
    App Store2016年最新审核规则
    iOS 根据字符串数目,自定义Label等控件的高度
    iOS 证书Bug The identity used to sign the executable is no longer valid 解决方案
    Entity FrameWork 增删查改的本质
    EF容器---代理类对象
    Entity FrameWork 延迟加载本质(二)
    Entity FrameWork 延迟加载的本质(一)
    Entity FrameWork 增删查改
  • 原文地址:https://www.cnblogs.com/libaoli/p/6278019.html
Copyright © 2011-2022 走看看