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!'); });
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
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!