【问题标题】:Has anyone used Node.JS to update MySQL有没有人用 Node.JS 更新 MySQL
【发布时间】:2016-06-23 18:00:54
【问题描述】:

我使用 AJAX 将数据“获取”到我的节点客户端,但我似乎无法弄清楚如何将这些数据“发布”到 MySQL。有什么想法吗?

【问题讨论】:

    标签: mysql node.js database express


    【解决方案1】:

    可以使用mysql npm模块:npm install mysql

    var mysql      = require('mysql');
    var pool = mysql.createPool({
        host: 'localhost'
        user: 'youruser'
        password: 'yourpassword' 
        database: 'yourdb'
    });
    
    pool.getConnection(function (err, connection) {
        connection.query('INSERT INTO tbl_name (col1,col2) VALUES(' + theValueYouGotFromAjax + ',' + anotherValue + ')' + '\'', function (err, result) {
                if (err) console.log(err);
                console.log('Created ' + result.affectedRows + ' rows');
                connection.release();
                console.log('Connection returned to pool.');
    });
    

    查看模块文档:https://github.com/mysqljs/mysql/tree/v0.9

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-01
      • 2018-03-19
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 2018-11-20
      • 2015-01-27
      • 1970-01-01
      相关资源
      最近更新 更多