MySQL數(shù)據(jù)庫node使用詳解
1 MySQL查詢對象
2 MySQL查詢數(shù)組
3 mysql2庫介紹使用
4 mysql2預(yù)處理語句
5 mysql2連接池使用
6 mysql2的Promi
這里僅說明如何使用服務(wù)器連接數(shù)據(jù)庫并進(jìn)行操作。
預(yù)處理語句就是可以輸入變量的語句(表現(xiàn)形式是有符號:?)。需要使用.execute來執(zhí)行;
需要運(yùn)行普通的語句(不添加變量的語句)。就使用query。
預(yù)處理語句有很多好處,比如性能好、安全性(sql注入)。
如果連接的用戶很多,每次都創(chuàng)建數(shù)據(jù)庫的連接和銷毀連接會有影響,所以創(chuàng)建數(shù)據(jù)庫連接的時候我們可以使用連接池來做優(yōu)化
沒使用連接池的連接方法:
使用了連接池的方法:
需要下載相應(yīng)的第三方庫才能讓node驅(qū)動數(shù)據(jù)庫:
npm install mysql2
準(zhǔn)備數(shù)據(jù)-將json文件的數(shù)據(jù)插入到數(shù)據(jù)庫中
從phpne.json文件里面獲取json格式的數(shù)據(jù)并寫到數(shù)據(jù)庫里面。
const mysql = require('mysql2'); const connection = mysql.createConnection({ host: 'localhost', port: 3306, user: 'root', password: 'Coderwhy123.', database: 'music_db' }); const statement = `INSERT INTO products SET ?;` const phoneJson = require('./phone.json'); for (let phone of phoneJson) { connection.query(statement, phone); }
phone.jsond的內(nèi)容:
[ { "brand": "華為", "title": "華為nova 3(全網(wǎng)通) ", "price": 2699, "score": 6.7, "voteCnt": 65, "url": "http://detail.zol.com.cn/cell_phone/index1185512.shtml", "pid": "1185512" }, { "brand": "華為", "title": "華為P20 Pro(6GB RAM/全網(wǎng)通) ", "price": 4488, "score": 8.3, "voteCnt": 103, "url": "http://detail.zol.com.cn/cell_phone/index1207038.shtml", "pid": "1207038" }, { "brand": "華為", "title": "華為P20(全網(wǎng)通) ", "price": 3388, "score": 8.4, "voteCnt": 127, "url": "http://detail.zol.com.cn/cell_phone/index1175779.shtml", "pid": "1175779" }, { "brand": "華為", "title": "華為nova 3i(4GB RAM/全網(wǎng)通) ", "price": 1999, "score": 7, "voteCnt": 9, "url": "http://detail.zol.com.cn/cell_phone/index1222100.shtml", "pid": "1222100" } ]
mysql2-基本使用
const mysql = require('mysql2') // 1.創(chuàng)建一個連接(連接上數(shù)據(jù)庫) const connection = mysql.createConnection({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.' }) // 2.執(zhí)行操作語句, 操作數(shù)據(jù)庫 const statement = 'SELECT * FROM `students`;' // structure query language: DDL/DML/DQL/DCL // query可以執(zhí)行DDL/DML/DQL/DCL的語句的代碼。返回的值在回調(diào)函數(shù)里面。 connection.query(statement, (err, values, fields) => { if (err) { console.log('查詢失敗:', err) return } // 查看結(jié)果 console.log(values) // console.log(fields) })
mysql2-預(yù)處理語句
const mysql = require('mysql2') // 1.創(chuàng)建一個連接 const connection = mysql.createConnection({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.' }) // 2.執(zhí)行一個SQL語句: 預(yù)處理語句 const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;' connection.execute(statement, [1000, 8], (err, values) => { console.log(values) }) // connection.destroy()
mysql2-連接池使用
const mysql = require('mysql2') // 1.創(chuàng)建一個連接 const connectionPool = mysql.createPool({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.', // connectionLimit用來限制連接數(shù)量的 connectionLimit: 5 }) // 2.執(zhí)行一個SQL語句: 預(yù)處理語句 const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;' connectionPool.execute(statement, [1000, 8], (err, values) => { console.log(values) })
mysql2-Promise寫法
const mysql = require('mysql2') // 1.創(chuàng)建一個連接 const connectionPool = mysql.createPool({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.', connectionLimit: 5 }) // 2.執(zhí)行一個SQL語句: 預(yù)處理語句 const statement = 'SELECT * FROM `products` WHERE price > ? AND score > ?;' connectionPool.promise().execute(statement, [1000, 9]).then((res) => { const [values, fields] = res console.log('-------------------values------------------') console.log(values) console.log('-------------------fields------------------') console.log(fields) }).catch(err => { console.log(err) })
到此這篇關(guān)于MySQL數(shù)據(jù)庫node使用的文章就介紹到這了,更多相關(guān)mysql node使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL存儲過程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout)
這篇文章主要介紹了MySQL存儲過程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout),存儲過程就是一組SQL語句集,功能強(qiáng)大,可以實(shí)現(xiàn)一些比較復(fù)雜的邏輯功能,類似于JAVA語言中的方法;Python里面的函數(shù)2022-07-07深入解析MySQL中的Redo Log、Undo Log和Binlog
本文詳細(xì)介紹了MySQL中的RedoLog、UndoLog和Binlog的背景、業(yè)務(wù)場景、功能、底層實(shí)現(xiàn)原理以及使用措施,通過Java代碼示例展示了如何與這些日志進(jìn)行交互,進(jìn)一步深化了對MySQL日志系統(tǒng)的理解,理解并合理使用這些日志,可以有效地提升數(shù)據(jù)庫的性能和可靠性2024-10-10mysql語句實(shí)現(xiàn)簡單的增、刪、改、查操作示例
這篇文章主要介紹了mysql語句實(shí)現(xiàn)簡單的增、刪、改、查操作,結(jié)合實(shí)例形式分析總結(jié)了mysql語句實(shí)現(xiàn)數(shù)據(jù)庫與表的創(chuàng)建、刪除以及增刪改查等常見操作技巧,需要的朋友可以參考下2019-05-05mysql-5.7.21-winx64免安裝版安裝--Windows 教程詳解
這篇文章主要介紹了mysql-5.7.21-winx64免安裝版安裝--Windows 教程詳解,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09MySQL?UPDATE多表關(guān)聯(lián)更新的實(shí)現(xiàn)示例
MySQL可以基于多表查詢更新數(shù)據(jù),本文主要介紹了MySQL?UPDATE多表關(guān)聯(lián)更新的實(shí)現(xiàn)示例,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08