Node????mysql???????
???????????? ???????[ 2016/12/30 11:22:24 ] ????????????? Node
???????Node??Web?????????????????? NoSQL ??????????????? MongoDB ???????????Щ????Web???????????????????????? MySQL ????????????????????????? MySQL ???????????????????????????
????npm install --save mysql
?????????????????? MySQL ?????????????????????????DOCS???????????????????????????
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost'??
user: 'me'??
password : 'secret'??
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution'?? function(err?? rows?? fields){
if (err) throw err;
console.log('The solution is: '?? rows[0].solution);
});
connection.end();
???????????????????????????????ó?????? createConnection(option) ????????????????????????????? connect() ?????????????????? query() ???????SQL???????????????????????? rows ????? rows ??????????
????1. ????
?????????????????????????????????Щ???????????? createConnection(option) ??? option ?? option ??????????????????????? createConnection() ???????????о??????????????
????host ??????
????user ?????????????
????password ????
????database ?????????
??????????????????????????1??DOCS?????????о????????????????Щ?????????
????2. ???
????????????????? end() ?????? end() ?????????????????????£?
????connect.end(function(err){
????console.log('End a connection');
????});
???????????????????? end() ????????????????????????????????????????????? destroy() ??????????????????????????????????ɡ?
????????????????
var mysql = require('mysql');
var option = require('./connect.js').option;
var conn = mysql.createConnection(option);
conn.query('select * from message'??function(err??rows??fields){
if(!err){
console.log(rows);
}
});
conn.end(function(err){
console.log('end a connection');
});
??????????????????? SELECT ????????????? end a connection ??????????????????? conn.destroy(); ?????????????κν????????????????????????????????
????3. ?????
?????????????????????????????????????????“????”??????????????????????“????”?????????????????????????????????????????????????????????????????????????????????????????????????DOCS???????????????
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10??
host : 'example.org'??
user : 'bob'??
password : 'secret'??
database : 'my_db'
});
pool.query('SELECT 1 + 1 AS solution'?? function(err?? rows?? fields){
if (err) throw err;
console.log('The solution is: '?? rows[0].solution);
});
??????????????????? createPool(option) ?? option ???????????? connectionLimit ????????????????????????????????????10?????????????????????????????????淽???????????
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org'??
user : 'bob'??
password : 'secret'??
database : 'my_db'
});
pool.getConnection(function(err?? connection){
// Use the connection
connection.query( 'SELECT something FROM sometable'?? function(err?? rows){
// And done with the connection.
connection.release();
// Don't use the connection here?? it has been returned to the pool.
});
// Use the connection
connection.query( 'SELECT something2 FROM sometable2'?? function(err?? rows){
// And done with the connection.
connection.release();
// Don't use the connection here?? it has been returned to the pool.
});
});
???????????????????????? query() ??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11