nodejs中關(guān)于mysql數(shù)據(jù)庫的操作
基本概念
為什么要有數(shù)據(jù)庫
沒有數(shù)據(jù)庫,我們的數(shù)據(jù)都是存儲(chǔ)在文件當(dāng)中的,那么文件存儲(chǔ)數(shù)據(jù)的缺點(diǎn)有:
- 文件的安全性問題。
- 文件不利于查詢和對數(shù)據(jù)的管理。
- 文件不利于存放海量數(shù)據(jù)
- 文件在程序中控制不方便
什么是數(shù)據(jù)庫
數(shù)據(jù)庫,簡而言之可視為電子化的文件柜——存儲(chǔ)電子文件的處所,用戶可以對文件中的數(shù)據(jù)運(yùn)行增加、刪除、修改、查詢等操作。
前端程序員只需要對數(shù)據(jù)庫有一定了解即可。
瀏覽器---->服務(wù)器---->數(shù)據(jù)庫
數(shù)據(jù)庫的分類
關(guān)系型數(shù)據(jù)庫:
MySQL、Oracle、SQL ServerSQLite(安卓)
非關(guān)系型數(shù)據(jù)庫
mongodbredisBigTableDBA
數(shù)據(jù)庫中基本術(shù)語
- 數(shù)據(jù)庫database:存放數(shù)據(jù)的倉庫,一般一個(gè)項(xiàng)目中的數(shù)據(jù)會(huì)存儲(chǔ)到一個(gè)數(shù)據(jù)庫中
- 表table: 一個(gè)表對應(yīng)一類數(shù)據(jù),比如學(xué)生表,老師表
- 列columns:一張表由多列組成,也叫一個(gè)字段,比如學(xué)生的姓名,成績,年齡等
- 行rows: 一個(gè)學(xué)生信息對應(yīng)一行,一行也叫一條記錄。
數(shù)據(jù)庫的可視化操作(創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表)
數(shù)據(jù)類型(部分)
int: 整數(shù)類型varchar: 字符類型datetime: 日期類型
數(shù)據(jù)庫的常見命令
SQL: 結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL 。用于數(shù)據(jù)庫的增刪改查以及管理等功能。
數(shù)據(jù)庫相關(guān)
--SQL中的注釋
SHOW DATABASES; 查看所有的數(shù)據(jù)CREATE DATABASE mydb;創(chuàng)建數(shù)據(jù)庫DROP DATABASE mydb;刪除數(shù)據(jù)庫USE mydb; 使用數(shù)據(jù)庫
表相關(guān)
SHOW TABLES;查看當(dāng)前數(shù)據(jù)庫中所有的表
創(chuàng)建表
CREATE TABLE user( ?? ?id INT auto_increment PRIMARY KEY, ?? ?name VARCHAR(255) NOT NULL, ?? ?age INT , ?? ?gender VARCHAR(4), ?? ?content VARCHAR(255) )
DROP TABLE user;刪除表
插入數(shù)據(jù)
INSERT INTO user (name, age, gender, content) VALUES ('胡聰聰', 18, '男', '哈哈哈,哈哈哈')
-- 如果省略列名,那么必須要和字段一一對應(yīng)
INSERT INTO user VALUES (null, '胡聰聰', 18, '男', '哈哈哈,哈哈哈')
?
INSERT INTO user SET name='hcc', age=18, gender='男', content='嘻嘻嘻'修改數(shù)據(jù)
// 修改所有的數(shù)據(jù) UPDATE USER SET name='胡西西' // 根據(jù)條件修改 UPDATE USER set name='胡聰聰', content="這是內(nèi)容" WHERE id = 2
刪除數(shù)據(jù)
// 刪除所有的數(shù)據(jù) DELETE FROM USER ? // 刪除id為5的數(shù)據(jù) DELETE FROM USER WHERE id = 5
查詢數(shù)據(jù)
-- 查詢所有數(shù)據(jù) SELECT * FROM user ? -- 查詢指定列 SELECT id, name,age from user
條件查詢
--- 并且
SELECT * from user where name='胡聰聰' AND age=21
?
--- 或者
SELECT * from user where name='胡聰聰' or age=21
?
-- 范圍查詢
?
-- 模糊查詢 ?%表示通配 ?_表示單個(gè)字符
SELECT * from user where name LIKE '胡%'
?
-- in語句
SELECT * from user where name in ('胡聰聰', 'hcc')
?
-- order by
-- 排序需要寫在最后面,,asc升序 ?desc:降序
SELECT * from user ORDER BY id desc
?
-- limit分頁
SELECT * from user ORDER BY id desc limit 3
SELECT * from user ORDER BY id desc limit 3,3
?
?
-- 獲取總條數(shù)
SELECT count(*) as total FROM user導(dǎo)入和導(dǎo)出數(shù)據(jù)庫腳本
node操作mysql
基本使用
安裝
npm install mysql
基本使用
// 導(dǎo)入第三方包
const mysql = require('mysql')
// 創(chuàng)建連接
var connection = mysql.createConnection({
? // 本地
? host: 'localhost',
? user: 'root',
? password: 'root',
? // 數(shù)據(jù)庫名稱
? database: 'mydb',
? port: 3306
})
?
// 連接數(shù)據(jù)庫
connection.connect()
?
// 執(zhí)行sql語句
connection.query('select * from user where id = 8', (err, result) => {
? if (err) return console.log('查詢失敗', err)
? // result返回的是數(shù)組, 數(shù)組中是一個(gè)對象
? console.log(result)
})
?
// 關(guān)閉連接
connection.end()查詢語句
var name = '胡聰聰'
// 使用?表示占位,可以防止sql注入
connect.query(`select * from user where name=?`, name, (err, result) => {
? if (err) return console.log('錯(cuò)誤了', err)
? console.log(result)
})插入語句
connect.query(
? 'insert into user (name, age, gender, content) values (?, ?, ?, ?)',
? ['胡嘻嘻', 18, '男', '哈哈哈哈'],
? err => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了')
? }
)
?
// 方式2
connect.query(
? 'insert into user set ?',
? {
? ? name: '胡洗洗',
? ? age: 30,
? ? gender: '男',
? ? content: '哈哈哈'
? },
? (err, result) => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了', result)
? }
)修改語句
connect.query(
? 'update user set ? where id = ?',
? [
? ? {
? ? ? name: '胡洗洗',
? ? ? age: 30,
? ? ? gender: '男',
? ? ? content: '哈哈哈'
? ? },
? ? 10
? ],
? (err, result) => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了', result)
? }
)刪除語句
connect.query('delete from user where id = ?', [10], (err, result) => {
? if (err) return console.log('失敗', err)
? console.log(result)
})db模塊封裝
// 導(dǎo)入mysql
const mysql = require('mysql')
?
// 創(chuàng)建連接對象
const connect = mysql.createConnection({
? host: 'localhost',
? port: 3306,
? user: 'root',
? password: 'root',
? database: 'mydb'
})
?
exports.query = function(sql, params, callback) {
? connect.connect()
? connect.query(sql, params, (err, data) => {
? ? callback && ?callback(err, data)
? })
? connect.end()
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 使用nodejs連接mySQL寫接口全過程(增刪改查)
- nodejs+mysql實(shí)現(xiàn)用戶相關(guān)的增刪改查的詳細(xì)操作
- 三分鐘教會(huì)你用nodejs操作mysql數(shù)據(jù)庫
- Nodejs?連接?mysql時(shí)報(bào)Error:?Cannot?enqueue?Query?after?fatal?error錯(cuò)誤的處理辦法
- NodeJs操作MYSQL方法詳細(xì)介紹
- Nodejs中koa2連接mysql的實(shí)現(xiàn)示例
- NodeJs+MySQL實(shí)現(xiàn)注冊登錄功能
- nodejs連接mysql數(shù)據(jù)庫及基本知識(shí)點(diǎn)詳解
- nodejs實(shí)現(xiàn)的連接MySQL數(shù)據(jù)庫功能示例
- Nodejs連接mysql并實(shí)現(xiàn)增、刪、改、查操作的方法詳解
- NodeJS連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作詳解
相關(guān)文章
koa-router路由參數(shù)和前端路由的結(jié)合詳解
這篇文章主要給大家介紹了關(guān)于koa-router路由參數(shù)和前端路由的結(jié)合的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用koa-router具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Node.js如何自動(dòng)審核團(tuán)隊(duì)的代碼
在項(xiàng)目開發(fā)中,統(tǒng)一團(tuán)隊(duì)的代碼風(fēng)格很重要,本文介紹如何用Node.js來自動(dòng)審核,來提高您的開發(fā)速度。2016-07-07
node.js支持多用戶web終端實(shí)現(xiàn)及安全方案
這篇文章主要介紹了node.js支持多用戶web終端實(shí)現(xiàn)方案以及web終端安全性保證的解決方法,一起學(xué)習(xí)參考下。2017-11-11
詳解如何模擬實(shí)現(xiàn)node中的Events模塊(通俗易懂版)
這篇文章主要介紹了如何模擬實(shí)現(xiàn)node中的Events模塊(通俗易懂版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
node.js調(diào)用腳本(python/shell)和系統(tǒng)命令
這篇文章介紹了node.js調(diào)用腳本(python/shell)和系統(tǒng)命令的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

