如何将MySQL连接到Node.js CRUD

时间:2018-10-01 04:42:57

标签: javascript mysql node.js windows

我一直在使用mongoDB。如何切换到MySQL?目前,我不知道在哪里看。我应该使用MySQL工作台吗? PGAdmin4?我该怎么做或学习如何做?

2 个答案:

答案 0 :(得分:0)

尝试JavaScript mysql package。这是在JavaScript中使用MySQL的最受欢迎的软件包,并且GitHub自述文件将引导您完成使用它的大多数步骤。

如果您想要第二个选项,我也有使用node-mysql软件包的经验(取决于mysql软件包)。使用起来有点简单,对我也很有效。

答案 1 :(得分:0)

我在这篇文章中添加了如何在Node.js中使用MySQL。

第1步:在MySQL数据库中创建表:

CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL COMMENT 'primary key',
  `employee_name` varchar(255) NOT NULL COMMENT 'employee name',
  `employee_salary` double NOT NULL COMMENT 'employee salary',
  `employee_age` int(11) NOT NULL COMMENT 'employee age'
);

第2步:使用此命令安装所有软件包:

npm install --save mysql express body-parser 

第3步:在同一目录中创建app.js文件

var http = require("http");
var express = require('express');
var app = express();
var mysql      = require('mysql');
var bodyParser = require('body-parser');

//start mysql connection
var connection = mysql.createConnection({
  host     : 'localhost', //mysql database host name
  user     : 'root', //mysql database user name
  password : '', //mysql database password
  database : 'dummy_db' //mysql database name
});

connection.connect(function(err) {
  if (err) throw err
  console.log('You are now connected...')
})
//end mysql connection

//start body-parser configuration
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));
//end body-parser configuration

//create app server
var server = app.listen(3000,  "127.0.0.1", function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)

});

//rest api to get all results
app.get('/employees', function (req, res) {
   connection.query('select * from employee', function (error, results, fields) {
      if (error) throw error;
      res.end(JSON.stringify(results));
    });
});

//rest api to get a single employee data
app.get('/employees/:id', function (req, res) {
   console.log(req);
   connection.query('select * from employee where id=?', [req.params.id], function (error, results, fields) {
      if (error) throw error;
      res.end(JSON.stringify(results));
    });
});

//rest api to create a new record into mysql database
app.post('/employees', function (req, res) {
   var postData  = req.body;
   connection.query('INSERT INTO employee SET ?', postData, function (error, results, fields) {
      if (error) throw error;
      res.end(JSON.stringify(results));
    });
});

//rest api to update record into mysql database
app.put('/employees', function (req, res) {
   connection.query('UPDATE `employee` SET `employee_name`=?,`employee_salary`=?,`employee_age`=? where `id`=?', [req.body.employee_name,req.body.employee_salary, req.body.employee_age, req.body.id], function (error, results, fields) {
      if (error) throw error;
      res.end(JSON.stringify(results));
    });
});

//rest api to delete record from mysql database
app.delete('/employees', function (req, res) {
   console.log(req.body);
   connection.query('DELETE FROM `employee` WHERE `id`=?', [req.body.id], function (error, results, fields) {
      if (error) throw error;
      res.end('Record has been deleted!');
    });
});

第4步:使用以下命令启动服务器:

 node app.js

如果遇到任何问题,请遵循此link