zoukankan      html  css  js  c++  java
  • Node.js连接mysql

    一、安装Node.js mysql驱动库

      Node.js里面没有mysql模块的,我们需要安装mysql模块。我们可以使用npm(Node package manager)进行安装。这里使用到的版本为:"2.0.0-rc2。安装步骤如下:

      1、打开cmd输入命令:npm install -g mysql 回车,下载完成之后效果图如下:

      mysql下载的目录为C:UsersuserAppDataRoaming pm ode_modules。

      2、将C:UsersuserAppDataRoaming pm ode_modules目录下的mysql目录拷贝到D:Program Files odejs ode_modules pm ode_modules这个目录下。D:Program Files为Nodejs的安装目录。这样就安装完成。

    二、编写连接mysql代码

      1、在D盘新建文本文件重命名为,testmysql.js。内容如下:

     1 var mysql = require('mysql');
     2 var TEST_DATABASE = 'nodejs_mysql_test';//数据库名
     3 var TEST_TABLE = 'user';//表名
     4 var client = mysql.createConnection({  
     5     host : 'localhost',  
     6     user : 'root',  
     7     password : '123456'
     8 }); 
     9 
    10 //创建数据库
    11 client.query('create database '+TEST_DATABASE, function(err) {
    12   if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
    13     throw err;
    14   }
    15 });
    16 
    17 client.query('USE '+TEST_DATABASE);//使用该数据库
    18 
    19 //创建表
    20 client.query(
    21   'CREATE TABLE '+TEST_TABLE+
    22   '(id INT(11) AUTO_INCREMENT, '+
    23   'username VARCHAR(255), '+
    24   'password VARCHAR(255), '+
    25   'created DATETIME, '+
    26   'PRIMARY KEY (id))'
    27 );
    28 
    29 
    30  //查询数据   
    31 var query = client.query('select * from  '+TEST_TABLE+' where id = ?', [2],function(err, results){ //查询id为2的数据
    32     console.log(results[0].id);//返回记录id
    33         console.log(results[0].username);//返回记录id
    34             console.log(results[0].password);//返回记录id
    35                 console.log(results[0].created);//返回记录id
    36   }
    37 ); 
    38 
    39  //添加数据
    40 var query = client.query('INSERT INTO '+TEST_TABLE+' SET username = ?, password = ?, created = ?',
    41   ['zhangsan', '1234', '2010-08-16 12:42:15'],function(err, results){
    42     console.log(results.insertId);//返回记录id
    43   }
    44 );
    45 
    46 
    47 //修改数据
    48 client.query("update user set password = ? where id = ?", [ "123456", 1 ], function(err, results) {//修改id为1的记录的password
    49   console.log(results);  
    50   /** result 为格式如下的信息
    51       {   fieldCount: 0,
    52           affectedRows: 1,
    53           insertId: 0,
    54           serverStatus: 2,
    55           warningCount: 0,
    56           message: '(Rows matched: 1  Changed: 1  Warnings: 0',
    57           protocol41: true,
    58           changedRows: 1 
    59       }
    60    */
    61 }); 
    62 
    63 //删除数据
    64 client.query("delete from user where id = ?", [ 1 ], function(err, results) {//删除id为1的记录
    65   console.log(results);  
    66   /** result 为格式如下的信息
    67       {
    68           fieldCount: 0,
    69           affectedRows: 1,
    70           insertId: 0,
    71           serverStatus: 2,
    72           warningCount: 0,
    73           message: '',
    74           protocol41: true,
    75           changedRows: 0 
    76       }
    77   */
    78 });
    79 
    80 client.end();//关闭连接

      cmd进入D盘 运行命令 node testmysql.js 回车即可运行。

  • 相关阅读:
    一些想说的事
    化学离子平衡作业偷懒神器
    solution
    SGU 刷题记
    INT128
    # 字典树的指针写法 1.
    CSP-S2 游记
    Tarjan 【整理】
    HGOI 20191106
    20191101
  • 原文地址:https://www.cnblogs.com/always-online/p/3487852.html
Copyright © 2011-2022 走看看