Node框架相关

1、connection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
//From this example, you can learn the following:
<font color="red">Every method you invoke on a connection is queued and executed in sequence.</font>
Closing the connection is done using end() which makes sure all remaining queries are executed <font color="red">before sending a quit packet to the mysql server.</font>

2、Transactions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO posts SET title=?', title, function (error, results, fields) {
if (error) {
return connection.rollback(function() {
throw error;
});
}
var log = 'Post ' + result.insertId + ' added';
connection.query('INSERT INTO log SET data=?', log, function (error, results, fields) {
if (error) {
return connection.rollback(function() {
throw error;
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});
// Please note that beginTransaction(), commit() and rollback() are simply convenience functions that <font color='red'> execute the START TRANSACTION, COMMIT, and ROLLBACK commands respectively.</font>

3、For your convenience, this driver will cast mysql types into native JavaScript types by default. If you are running into problems, one thing that may help is enabling the debug mode for the connection.
4、
5、

Comments

去留言
2017-06-12

⬆︎TOP