MySQL: Difference between revisions
Kostya nad (talk | contribs) (Created page with " == Plugin == MySQL is a NodeJS plugin. You must download NodeJS recommend version fron [https://nodejs.org/en/ official site (https://nodejs.org/en/)] and install it with <co...") |
Kostya nad (talk | contribs) |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 8: | Line 8: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
So. Functions for working with | So. Functions for working with MySQL: | ||
=== createConnection === | === createConnection === | ||
Create information for connection but do not open it. | Create information for connection but do not open it. | ||
| Line 16: | Line 16: | ||
user:'kostya_nad',// MySQL user | user:'kostya_nad',// MySQL user | ||
password:'MyStrongPassword',// MySQL password | password:'MyStrongPassword',// MySQL password | ||
database:' | database:'gmgta'// MySQL database | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== connect === | === connect === | ||
Open connection to database | Open connection to database | ||
| Line 24: | Line 25: | ||
mysqlc.connect(function(e) { | mysqlc.connect(function(e) { | ||
if(e) { | if(e) { | ||
console.log("Error connecting to the database | console.log("Error connecting to the database with error "+e); | ||
} | } | ||
else { | else { | ||
| Line 32: | Line 32: | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== end === | === end === | ||
Close connection to database | Close connection to database | ||
Latest revision as of 17:00, 6 November 2017
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:'gmgta'// 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);
}
});