天天看點

在項目中操作 MySQL一. 在項目中操作 MySQL

一. 在項目中操作 MySQL

1.1 在項目中操作資料庫的步驟

①安裝操作 MySQL 資料庫的第三方子產品(mysql)

②通過 mysql 子產品連接配接到 MySQL 資料庫

③通過 mysql 子產品執行 SQL 語句

在項目中操作 MySQL一. 在項目中操作 MySQL

1.2 安裝與配置 mysql 子產品

1. 安裝 mysql 子產品

mysql 子產品是托管于 npm 上的第三方子產品。它提供了在 Node.js 項目中連接配接和操作 MySQL 資料庫的能力。

想要在項目中使用它,需要先運作如下指令,将 mysql 安裝為項目的依賴包:

npm i mysql
           

2. 配置 mysql 子產品

在使用 mysql 子產品操作 MySQL 資料庫之前,必須先對 mysql 子產品進行必要的配置,主要的配置步驟如下:

// 1.導入 MySQL 資料庫
const mysql = require('mysql');
// 2.建立與 MySQL 資料庫的連結關系
const db = mysql.createPool({
    host: '127.0.0.1', // 資料庫的 IP 位址
    user: 'root', // 登入資料庫的賬号
    password: 'root', // 登入資料庫的密碼
    database: '本地測試MySQL', // 指定要操作的資料庫
});
           

3. 測試 mysql 子產品能否正常工作

// 檢測 mysql 能否正常工作
db.query('select 1', (err, results) => {
    // mysql 工作期間報錯
    if (err) return console.log(err.message);
    // 能夠成功執行 SQL 語句
    // 隻要執行結果顯示 [ RowDataPacket { '1': 1 } ] 就表明成功
    console.log(results);
});
           

1.3 使用 mysql 子產品操作 MySQL 資料庫

1. 查詢資料

查詢 students 表中所有的資料:

// 查詢 students 表中的資料
const sqlStr = 'select * from students';
db.query(sqlStr, (err, results) => {
    // 查詢失敗
    if (err) return console.log(err.message);
    // 查詢成功 
    // 如果執行 select 語句成功,傳回的是一個數組
    console.log(results);
});
           

2. 插入資料

向 students 表中新增資料。示例代碼如下:

// 向 students 表中添加一條資料
const adddate = { name: '段仙子', age: 17, addres: '海王星', num: '18070045' };
// 定義待執行的 SQL 語句
const sqlStr = 'insert into students (name,age,addres,num) value (?,?,?,?)';
// 執行 SQL 語句
db.query(sqlStr, [adddate.name, adddate.age, adddate.addres, adddate.num], (err, results) => {
    // 查詢失敗
    if (err) return console.log(err.message);
    // 查詢成功
    // 注意:如果執行 insert into 語句成功,傳回的是一個對象
    // 可以通過 affectedRows 屬性判斷是否執行成功 
    if (results.affectedRows === 1) {
        console.log('添加資料成功!');
    };
});
           

3. 插入資料的便捷方式

向表中新增資料時,如果資料對象的每個屬性和資料表的字段一一對應,則可以通過如下方式快速插入資料:

// 向 students 表中添加一條資料(簡便方式)
const adddate = { name: '段仙子', age: 17, addres: '金星', num: '18070044' };
// 定義簡便的 SQL 語句
const sqlStr = 'insert into students set ?';
// 執行 SQL 語句
db.query(sqlStr, adddate, (err, results) => {
    // 失敗後
    if (err) return console.log(err.message);
    // 成功後
    console.log(results);
    if (results.affectedRows) {
        console.log('執行成功!');
    };
});
           

4. 更新資料

可以通過如下方式,更新表中的資料:

// 更新 students 表中的資料
const adddate = { id: 4, age: 22, addres: '水星' };
// 定義簡便的 SQL 語句
const sqlStr = 'update students set age=?,addres=? where id=?';
// 執行 SQL 語句
db.query(sqlStr, [adddate.age, adddate.addres, adddate.id], (err, resulter) => {
    // 執行失敗
    if (err) return console.log(err.message);
    // 執行成功
    // 注意:執行 update 語句成功後,傳回的也是一個對象
    // 可以通過 affectedRows 屬性判斷是否成功
    if (resulter.affectedRows) {
        console.log('執行成功!');
    };
});
           

5. 更新資料的便捷方式

更新表資料時,如果資料對象的每個屬性和資料表的字段一一對應,則可以通過如下方式快速更新表資料:

// 更新 students 表中的資料便捷方式
const adddate = { id: 5, age: 23, addres: '天王星' };
// 定義便捷的 SQL 語句
const sqlStr = 'update students set ? where id=?';
// 執行 SQL 語句
db.query(sqlStr, [adddate, adddate.id], (err, resulter) => {
    // 執行失敗
    if (err) return console.log(err.message);
    // 執行成功
    if (resulter.affectedRows) {
        console.log('執行成功!');
    };
});
           

6. 删除資料

在删除資料時,推薦根據 id 這樣的唯一辨別,來删除對應的資料。示例如下:

// 删除 students 表中的資料
const sqlStr = 'delete from students where id=?';
// 執行指令
// 注意:如果 SQL 語句中有多個占位符,則必須使用數組為每個占位符指定具體的值
// 		如果 SQL 語句隻有一個占位符,則可以省略數組。
db.query(sqlStr, 3, (err, resulter) => {
    // 執行失敗
    if (err) return console.log(err.message);
    // 執行成功
    if (resulter.affectedRows === 1) {
        console.log('删除成功!');
    };
});
           

7. 标記删除

使用 DELETE 語句,會把真正的把資料從表中删除掉。為了保險起見,推薦使用标記删除的形式,來模拟删除的動作。

所謂的标記删除,就是在表中設定類似于 status 這樣的狀态字段,來标記目前這條資料是否被删除。

當使用者執行了删除的動作時,我們并沒有執行 DELETE 語句把資料删除掉,而是執行了 UPDATE 語句,将這條資料對應的 status 字段标記為删除即可。

// 使用标記删除 students 表中的資料
const sqlStr = 'update students set status=1 where id=?';
// 執行語句
db.query(sqlStr, 2, (err, resulter) => {
    // 執行失敗
    if (err) return console.log(err.message);
    // 執行成功
    if (resulter.affectedRows === 1) {
        console.log('删除成功!');
    };
});