MySQL數(shù)據(jù)庫(kù)node使用詳解
1 MySQL查詢對(duì)象
2 MySQL查詢數(shù)組
3 mysql2庫(kù)介紹使用
4 mysql2預(yù)處理語(yǔ)句
5 mysql2連接池使用
6 mysql2的Promi
這里僅說(shuō)明如何使用服務(wù)器連接數(shù)據(jù)庫(kù)并進(jìn)行操作。
預(yù)處理語(yǔ)句就是可以輸入變量的語(yǔ)句(表現(xiàn)形式是有符號(hào):?)。需要使用.execute來(lái)執(zhí)行;
需要運(yùn)行普通的語(yǔ)句(不添加變量的語(yǔ)句)。就使用query。
預(yù)處理語(yǔ)句有很多好處,比如性能好、安全性(sql注入)。
如果連接的用戶很多,每次都創(chuàng)建數(shù)據(jù)庫(kù)的連接和銷毀連接會(huì)有影響,所以創(chuàng)建數(shù)據(jù)庫(kù)連接的時(shí)候我們可以使用連接池來(lái)做優(yōu)化
沒(méi)使用連接池的連接方法:
使用了連接池的方法:
需要下載相應(yīng)的第三方庫(kù)才能讓node驅(qū)動(dòng)數(shù)據(jù)庫(kù):
npm install mysql2準(zhǔn)備數(shù)據(jù)-將json文件的數(shù)據(jù)插入到數(shù)據(jù)庫(kù)中
從phpne.json文件里面獲取json格式的數(shù)據(jù)并寫到數(shù)據(jù)庫(kù)里面。
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)建一個(gè)連接(連接上數(shù)據(jù)庫(kù))const connection = mysql.createConnection({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.'})// 2.執(zhí)行操作語(yǔ)句, 操作數(shù)據(jù)庫(kù)const statement = 'SELECT * FROM `students`;'// structure query language: DDL/DML/DQL/DCL// query可以執(zhí)行DDL/DML/DQL/DCL的語(yǔ)句的代碼。返回的值在回調(diào)函數(shù)里面。connection.query(statement, (err, values, fields) => { if (err) { console.log('查詢失敗:', err) return } // 查看結(jié)果 console.log(values) // console.log(fields)})mysql2-預(yù)處理語(yǔ)句
const mysql = require('mysql2')// 1.創(chuàng)建一個(gè)連接const connection = mysql.createConnection({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.'})// 2.執(zhí)行一個(gè)SQL語(yǔ)句: 預(yù)處理語(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)建一個(gè)連接const connectionPool = mysql.createPool({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.', // connectionLimit用來(lái)限制連接數(shù)量的 connectionLimit: 5})// 2.執(zhí)行一個(gè)SQL語(yǔ)句: 預(yù)處理語(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)建一個(gè)連接const connectionPool = mysql.createPool({ host: 'localhost', port: 3306, database: 'music_db', user: 'root', password: 'Coderwhy123.', connectionLimit: 5})// 2.執(zhí)行一個(gè)SQL語(yǔ)句: 預(yù)處理語(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ù)庫(kù)node使用的文章就介紹到這了,更多相關(guān)mysql node使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
