有没有人用Node.JS来更新MySQL

时间:2016-06-23 18:00:54

标签: mysql node.js database express

我使用AJAX将数据“获取”到我的Node Client中,但我似乎无法弄清楚如何将这些数据“POST”到MySQL。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用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