美文网首页
node.js如何连接mysql

node.js如何连接mysql

作者: 吴博 | 来源:发表于2017-05-26 21:44 被阅读0次
    var mysql = require('mysql');
    
    var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'test',
    port: '3306'
    });
    connection.connect();
    
    var userInfo = null;
    
    connection.query("select * from user", function (err, items) {
    if (err)  console.log(err);
    console.log(items);
    });
    
    var item = {id: 2, name: 'gc', desc: 'haha'};
    connection.query("insert into user set ?", item, function (err, result) {
    console.log(result + '\n');
    });
    
    connection.end();
    

    增删改查都可以做到.

    相关文章

      网友评论

          本文标题:node.js如何连接mysql

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