Home > Web Front-end > JS Tutorial > Example of simple nodejs method to access and operate mysql database

Example of simple nodejs method to access and operate mysql database

亚连
Release: 2018-05-30 10:28:17
Original
1509 people have browsed it

This article mainly introduces the method of nodejs to simply access and operate the mysql database. It analyzes the related operation skills of nodejs to create mysql connection, execute sql statement and close the connection in the form of examples. Friends in need can refer to the following

The example in this article describes how nodejs can simply access and operate the mysql database. Share it with everyone for your reference, the details are as follows:

var mysql = require('mysql'); //调用MySQL模块 mysql模块要安装 $ npm install mysql
//创建一个connection
var connection = mysql.createConnection({
  host   : '127.0.0.1',    //主机
  user   : 'root',        //MySQL认证用户名
  password : '',    //MySQL认证用户密码
  port: '3306',          //端口号
  database:''   //数据库名
});
//创建一个connection
connection.connect(function(err){
  if(err){
    console.log('[query] - :'+err);
    return;
  }
  console.log('[connection connect] succeed!');
});
//执行SQL语句
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) {
    console.log('[query] - :'+err);
    return;
  }
  console.log('The solution is: ', rows[0].solution);
});
//关闭connection
connection.end(function(err){
  if(err){
    return;
  }
  console.log('[connection end] succeed!');
});
Copy after login

The above is what I compiled for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Introduction to the calling sequence of functions in vue

The function of uploading images to the database and displaying them on the page implemented by vue Example

JS implementation of adding event operations to dynamically created elements Example

The above is the detailed content of Example of simple nodejs method to access and operate mysql database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template