MySQL
Plugin
MySQL is a NodeJS plugin. You must download NodeJS recommend version fron official site (https://nodejs.org/en/) and install it with
npm install mysql
OK. We installed MySQL. In our index.js write this:
var mysql = require('mysql');
So. Functions for working with MySQL:
createConnection
Create information for connection but do not open it.
var mysqlc = mysql.createConnection({
host:'127.0.0.1',// host of server
user:'kostya_nad',// MySQL user
password:'MyStrongPassword',// MySQL password
database:'copsandnotacops'// MySQL database
});
connect
Open connection to database
mysqlc.connect(function(e) {
if(e) {
console.log("Error connecting to the database with error "+e);
}
else {
console.log('Database connected!')
}
});
end
Close connection to database
mysqlc.end();
query
Send a MySQL query to database
mysqlc.query("SELECT * FROM `users` WHERE `nickname`=", [], function(e, r) {
if(e) {
console.log('Error on connection ... ');
throw e;
}
else {
console.log('Password is '+r[0].passcode);
}
});