美文网首页
学习Node.js(连接 MySQL)

学习Node.js(连接 MySQL)

作者: 薥劃 | 来源:发表于2020-04-07 15:29 被阅读0次

    安装数据库驱动

    $ cnpm install mysql

    连接数据库

    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : '',
      user     : '',
      password : '',
      database : ''
    });
     
    connection.connect();
     
    connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
      if (error) throw error;
      console.log('The solution is: ', results[0].solution);
    });
    

    数据库操作( CURD )

    查询数据
    var mysql  = require('mysql');  
     
    var connection = mysql.createConnection({     
      host     : '',       
      user     : '',              
      password : '',       
      port: '',                   
      database: '' 
    }); 
     
    connection.connect();
     
    var  sql = 'SELECT * FROM websites';
    //查
    connection.query(sql,function (err, result) {
            if(err){
              console.log('[SELECT ERROR] - ',err.message);
              return;
            }
     
           console.log('--------------------------SELECT----------------------------');
           console.log(result);
           console.log('------------------------------------------------------------\n\n');  
    });
     
    connection.end();
    
    插入数据
    var mysql  = require('mysql');  
     
    var connection = mysql.createConnection({     
      host     : '',       
      user     : '',              
      password : '',       
      port: '',                   
      database: '' 
    }); 
     
    connection.connect();
     
    var  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)';
    var  addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN'];
    //增
    connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
             console.log('[INSERT ERROR] - ',err.message);
             return;
            }        
     
           console.log('--------------------------INSERT----------------------------');
           //console.log('INSERT ID:',result.insertId);        
           console.log('INSERT ID:',result);        
           console.log('-----------------------------------------------------------------\n\n');  
    });
     
    connection.end();
    
    更新数据
    var mysql  = require('mysql');  
     
    var connection = mysql.createConnection({     
      host     : '',       
      user     : '',              
      password : '',       
      port: '',                   
      database: '' 
    }); 
     
    connection.connect();
     
    var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?';
    var modSqlParams = ['菜鸟移动站', 'https://m.runoob.com',6];
    //改
    connection.query(modSql,modSqlParams,function (err, result) {
       if(err){
             console.log('[UPDATE ERROR] - ',err.message);
             return;
       }        
      console.log('--------------------------UPDATE----------------------------');
      console.log('UPDATE affectedRows',result.affectedRows);
      console.log('-----------------------------------------------------------------\n\n');
    });
     
    connection.end();
    
    删除数据
    var mysql  = require('mysql');  
     
    var connection = mysql.createConnection({     
      host     : '',       
      user     : '',              
      password : '',       
      port: '',                   
      database: '' 
    }); 
     
    connection.connect();
     
    var delSql = 'DELETE FROM websites where id=6';
    //删
    connection.query(delSql,function (err, result) {
            if(err){
              console.log('[DELETE ERROR] - ',err.message);
              return;
            }        
     
           console.log('--------------------------DELETE----------------------------');
           console.log('DELETE affectedRows',result.affectedRows);
           console.log('-----------------------------------------------------------------\n\n');  
    });
     
    connection.end();
    

    相关文章

      网友评论

          本文标题:学习Node.js(连接 MySQL)

          本文链接:https://www.haomeiwen.com/subject/bcrnphtx.html