学习目标

  • 使用 mysql 包操作 MySQL 数据库
  • 使用数据库的动态网站
  • 基于数据库的网站(增删改查)

使用 mysql 包

安装

  1. npm install mysql

Hello World

  1. var mysql = require("mysql");
  2. var connection = mysql.createConnection({
  3. host: "localhost",
  4. user: "me",
  5. password: "secret",
  6. database: "my_db"
  7. });
  8. connection.connect();
  9. connection.query("SELECT 1 + 1 AS solution", function(error, results, fields) {
  10. if (error) throw error;
  11. console.log("The solution is: ", results[0].solution);
  12. });
  13. connection.end();

增删改查

查询

基本查询:

  1. connection.query('SELECT * FROM `books` WHERE `author` = "David"', function(
  2. error,
  3. results,
  4. fields
  5. ) {
  6. // error will be an Error if one occurred during the query
  7. // results will contain the results of the query
  8. // fields will contain information about the returned results fields (if any)
  9. });

条件查询:

  1. connection.query(
  2. "SELECT * FROM `books` WHERE `author` = ?",
  3. ["David"],
  4. function(error, results, fields) {
  5. // error will be an Error if one occurred during the query
  6. // results will contain the results of the query
  7. // fields will contain information about the returned results fields (if any)
  8. }
  9. );

添加

  1. var post = { id: 1, title: "Hello MySQL" };
  2. var query = connection.query("INSERT INTO posts SET ?", post, function(
  3. error,
  4. results,
  5. fields
  6. ) {
  7. if (error) throw error;
  8. // Neat!
  9. });
  10. console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

删除

  1. connection.query('DELETE FROM posts WHERE title = "wrong"', function(
  2. error,
  3. results,
  4. fields
  5. ) {
  6. if (error) throw error;
  7. console.log("deleted " + results.affectedRows + " rows");
  8. });

修改

  1. connection.query(
  2. "UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?",
  3. ["a", "b", "c", userId],
  4. function(error, results, fields) {
  5. if (error) throw error;
  6. // ...
  7. }
  8. );

连接池

  1. var mysql = require("mysql");
  2. var pool = mysql.createPool({
  3. host: "example.org",
  4. user: "bob",
  5. password: "secret",
  6. database: "my_db",
  7. connectionLimit: 10 // 默认是 10 个
  8. });
  9. pool.getConnection(function(err, connection) {
  10. // Use the connection
  11. connection.query("SELECT something FROM sometable", function(
  12. error,
  13. results,
  14. fields
  15. ) {
  16. // 释放回连接池
  17. connection.release();
  18. // 处理错误
  19. if (error) throw error;
  20. // ...
  21. });
  22. });

封装 dbHelper.js

  1. const mysql = require("mysql");
  2. const pool = mysql.createPool({
  3. host: "localhost",
  4. user: "root",
  5. password: "123456",
  6. database: "test",
  7. connectionLimit: 10 // 默认是 10 个
  8. });
  9. exports.query = (...args) => {
  10. // 从数组中弹出最后一个元素 callback 回调函数
  11. const callback = args.pop();
  12. pool.getConnection((err, connection) => {
  13. if (err) {
  14. return callback(err);
  15. }
  16. connection.query(...args, function(...results) {
  17. // ...results => [err, results, fields]
  18. // 释放回连接池
  19. connection.release();
  20. // 把 ...results => [err, results, fields] 展开调用 callback 继续往外抛
  21. callback(...results);
  22. });
  23. });
  24. };

结合数据库的网站