zoukankan      html  css  js  c++  java
  • Node操作MySql数据库

      MySql数据库是最常用的数据库,node也提供了操作mysql数据库的模块:mysql。

      安装mysql模块:npm install mysql

      

     JavaScript Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
     
    /* MySql模块学习
        //创建数据库
        Create DATABASE MYSQLDATA;

        //表操作
        Create Table MYTABLE (name VARCHAR(20), sex CHAR(1));
        Insert into MYTABLE Values('Michael', 'm');
        Update MYTABLE Set name = 'Michael Jordan' Where sex = 'm';
        Select * From MYTABLE
        Delete from MYTABLE;
        Drop Table MYTABLE;
        
        //删除数据库
        Drop DATABASE MYSQLDATA;
     */

    // 引入Mysql
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : 
    'localhost',
      user     : 
    'root',
      password : 
    'kingview',
      database : 
    'test'
    });
    /* MySql数据库连接参数说明:
    参数                  描述
    host                    主机地址 (默认:localhost)
    user                    用户名
    password                密码
    port                    端口号 (默认:3306)
    database                数据库名
    charset                 连接字符集(默认:'UTF8_GENERAL_CI',注意字符集的字母都要大写)
    localAddress            此IP用于TCP连接(可选)
    socketPath              连接到unix域路径,当使用 host 和 port 时会被忽略
    timezone                时区(默认:'local')
    connectTimeout          连接超时(默认:不限制;单位:毫秒)
    stringifyObjects        是否序列化对象
    typeCast                是否将列值转化为本地JavaScript类型值 (默认:true)
    queryFormat             自定义query语句格式化方法
    supportBigNumbers       数据库支持bigint或decimal类型列时,需要设此option为true (默认:false)
    bigNumberStrings        supportBigNumbers和bigNumberStrings启用 强制bigint或decimal列以JavaScript字符串类型返回(默认:false)
    dateStrings             强制timestamp,datetime,data类型以字符串类型返回,而不是JavaScript Date类型(默认:false)
    debug                   开启调试(默认:false)
    multipleStatements      是否许一个query中有多个MySQL语句 (默认:false)
    flags                   用于修改连接标志
    ssl                     使用ssl参数(与crypto.createCredenitals参数格式一至)或一个包含ssl配置文件名称的字符串,目前只捆绑Amazon RDS的配置文件
     */


    // 执行数据库连接 
    connection.connect();

    var sqlstring = "";
     
    // 创建表
    sqlstring = "Create Table MYTABLE (name VARCHAR(20), sex CHAR(1))"
    connection.query(sqlstring, 
    function (err, results, fields) {
        
    if (err) {
             console.log(
    '[UPDATE ERROR] - ', err.message);
            
    return;
        }
        console.log(
    '--------------------------CREATE----------------------------');       
        console.log(
    'CREATE TABLE:', results);        
        console.log(
    '------------------------------------------------------------ ');  
    });

    // 插入数据
    sqlstring = "Insert into MYTABLE Values('Michael', 'm')";
    connection.query(sqlstring, 
    function (err, result) {
        
    if(err){
            console.log(
    '[INSERT ERROR] - ', err.message);
            
    return;
        }        
     
       console.log(
    '--------------------------INSERT----------------------------');       
       console.log(
    'INSERT ID - ', result);        
       console.log(
    '------------------------------------------------------------ ');  
    });

    // 更新数据
    sqlstring = "Update MYTABLE Set name = 'Michael Jordan' Where sex = 'm'";
    connection.query(sqlstring, 
    function (err, result) {
        
    if(err){
            console.log(
    '[UPDATE ERROR] - ', err.message);
            
    return;
        }        
        console.log(
    '--------------------------UPDATE----------------------------');
        console.log(
    'UPDATE affectedRows - ', result.affectedRows);
        console.log(
    '------------------------------------------------------------ ');  
    });

    // 查询数据
    sqlstring = "Select * From MYTABLE";
    connection.query(sqlstring, 
    function (err, result) {
        
    if(err){
            console.log(
    '[SELECT ERROR] - ', err.message);
            
    return;
        }
     
        console.log(
    '--------------------------SELECT---------------------------');
        console.log(
    'SELECT - ', result);
        console.log(
    '------------------------------------------------------------ ');   
    });

    //删除数据
    sqlstring = "Delete From MYTABLE";
    connection.query(sqlstring, 
    function (err, result) {
        
    if(err){
            console.log(
    '[DELETE ERROR] - ', err.message);
            
    return;
        }        
     
        console.log(
    '--------------------------DELETE----------------------------');
        console.log(
    'DELETE affectedRows - ', result.affectedRows);
        console.log(
    '------------------------------------------------------------ ');  
    });

    //删除表格
    sqlstring = "Drop Table MYTABLE";
    connection.query(sqlstring, 
    function (err, result) {
        
    if(err){
            console.log(
    '[DROP ERROR] - ', err.message);
            
    return;
        }        
     
        console.log(
    '--------------------------DROP-----------------------------');
        console.log(
    'DROP TABLE :', result.affectedRows);
        console.log(
    '------------------------------------------------------------ ');    
    });


    // 断开数据库连接
    connection.end();

  • 相关阅读:
    建设全功能团队
    gwt之mvc4g
    SQLServer代码差异备份
    DateTimePicker选择到秒
    SQL定时备份并删除过期备份
    Div文本垂直居中
    WinForm当前窗口抓图
    web架构设计经验分享(转)
    用yui compressor 压缩 javascirpt脚本
    查看sqlserver连接数
  • 原文地址:https://www.cnblogs.com/MakeView660/p/7920149.html
Copyright © 2011-2022 走看看