node怎么操作MySQL數(shù)據(jù)庫?下面本篇文章帶大家了解一下node項目中MySQL數(shù)據(jù)庫增刪改查的方法,希望對大家有所幫助!
net start mysql
啟動mysql可在桌面右鍵我的電腦進入計算機管理查看mysql是否已經(jīng)成功運行 【相關(guān)教程推薦:nodejs視頻教程】功能:為我們提供連接、操作mysql數(shù)據(jù)庫的功能
www.navicat.com.cn/products#na…
(資料圖片僅供參考)
雙擊,一路next
找到應(yīng)用程序,點擊啟動
如果 連接測試通過,接下來就可以點擊確定按鈕,正式連入mysql了。
連入后的效果如下:
英文: database保存和管理數(shù)據(jù)的倉庫就是數(shù)據(jù)庫。
什么是數(shù)據(jù)? 文件,圖片,視頻,訂單,用戶名,密碼等等。
這些數(shù)據(jù)都需要有專門的地方來保存和管理。
在我們沒有學習數(shù)據(jù)庫技術(shù)之前,我們使用的數(shù)據(jù)都是以文件系統(tǒng)(db.json)的方式保存的。我們需要一個專門的軟件來管理我們的數(shù)據(jù), 這就是數(shù)據(jù)庫。
關(guān)系型數(shù)據(jù)庫,代表產(chǎn)品:
MySQLOracleSql serverDB2非關(guān)系型數(shù)據(jù)庫
redis鍵值存儲數(shù)據(jù)庫HBaise列存儲數(shù)據(jù)庫mongodb面向文檔數(shù)據(jù)庫neo4j 圖形數(shù)據(jù)庫Elasticsearch 搜索引擎存儲參考:數(shù)據(jù)庫使用排名 db-engines.com/en/ranking
在關(guān)系型數(shù)據(jù)庫中,存在三級關(guān)系:
數(shù)據(jù)庫數(shù)據(jù)表字段類比excel:
每一列都是一類數(shù)據(jù) ---字段每一行代表一條數(shù)據(jù) --- 記錄| 數(shù)據(jù)庫 | excel文件 |
|---|---|
| 數(shù)據(jù)庫 | excel文件 |
| 數(shù)據(jù)表 | excel文件中的某一個sheet |
| 表結(jié)構(gòu):字段 | sheet中的表頭:列 |
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下產(chǎn)品 。MySQL所使用的 SQL 語言是用于訪問數(shù)據(jù)庫的最常用標準化語言。
體積小、速度快、總體擁有成本低,一般中小型網(wǎng)站的開發(fā)都選擇 MySQL 作為網(wǎng)站數(shù)據(jù)庫。搭配 PHP 和 Apache 可組成良好的開發(fā)環(huán)境。3p技術(shù):php,asp,jsp
在彈出的窗口中填寫數(shù)據(jù)庫名即可。
點擊確定之后,會在左側(cè)的數(shù)據(jù)庫列中看到上面創(chuàng)建成功的數(shù)據(jù)庫。
點擊 "新建表", 然后在開始設(shè)置字段
設(shè)置字段完成之后,點擊上圖左上角所示的保存按鈕,就會進一步彈出對話框,讓填寫表的名字。
我們填入user
一條內(nèi)容輸入完成后,按下tab,會自動進入下一條記錄的輸入
結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL,用來操作關(guān)系型數(shù)據(jù)庫:
是一種數(shù)據(jù)庫查詢和程序設(shè)計語言,用來存取數(shù)據(jù)以及查詢、更新、和管理關(guān)系型數(shù)據(jù)庫。.sql是數(shù)據(jù)庫腳本文件的擴展名。最常用的用于數(shù)據(jù)操作的sql語句有四類,分別對應(yīng)對數(shù)據(jù)的四種操作:
增(create)(例如:用戶注冊)刪(delete) (例如:刪除訂單)改(update) (例如:修改密碼)查(select , read) (例如:信息搜索)然后:
格式:
insert into 表名(字段名1,字段名2,....) values (值1,值2,....)
注意:
字段的順序要和值的順序是完全匹配的
字段列表可以不與真實數(shù)據(jù)表中的字段完全相等,
可以省略一些不必要的字段順序與不需要與定義表時的順序一致如果是字符串類型的字段,其值要加"",如果是數(shù)值類型的字符串,其值不需要加“”
示例:
insert into stu (sex, weight, name) values ("男", 60, "龐凱")delete from 表名 where 刪除條件復(fù)制代碼
注意:不指定條件將刪除所有數(shù)據(jù)
-- 刪除id為14的同學delete from stu where id=14-- 刪除的時候,不加條件,將刪除stu表中的全部記錄delete from stu
update 表名 set 字段1=值1, 字段2=值2,... where 修改條件
注意:
- 要修改的值使用鍵值對來表示 - 多個字段用,分隔- 不指定條件,將修改當前表中全部的記錄
-- 修改id為1的同學的年齡為53update stu set age=53 where id = 1-- 修改id為1的同學的年齡為35,身高為160update stu set age=35,height=160 where id = 1-- 如果修改的時候,不加條件,則會修改全部的數(shù)據(jù)update stu set weight = 60
把數(shù)據(jù)從數(shù)據(jù)庫查出來
SELECT 字段名1, 字段名2, ..... FROM 表名 WHERE <條件表達式>
# 查詢部分字段SELECT id,name,age FROM stu# 查詢所有字段SELECT * FROM stu# 帶條件的查詢SELECT * FROM 表名 WHERE 條件1 and 條件2
select field1, field2... from 表名 查詢表中的所有數(shù)據(jù)
where 可以使用條件來篩選查詢出的結(jié)果
-- 查詢所有的學生select * from stu-- 查詢所有學生的id,name,heightselect id,name,height from stu-- 帶條件的查詢select * from stu where 條件-- 查詢所有的男同學select * from stu where sex="男"-- 查詢id為2的男同學select * from stu where id=2-- 查詢年齡大于50的同學select * from stu where age > 50-- 查詢年齡大于50歲的男同學select * from stu where age>50 and sex="男"-- 查詢年齡在30~60之間的同學,包括30和60select * from stu where age>=30 and age<=60select * from stu where age between 30 and 60
通過mysql這個包來操作mysql數(shù)據(jù)庫。
mysql模塊是一個第三方模塊,專門用來操作MySQL數(shù)據(jù)庫。
# 安裝npm i mysql
參考:www.npmjs.com/package/mys…
要想用這個包連接數(shù)據(jù)庫,首先要確保在電腦有mysql(phpstudy 還要啟動mysql服務(wù))
一共需要4個步驟:
加載 MySQL 模塊
創(chuàng)建 MySQL 連接對象
連接 MySQL 服務(wù)器
執(zhí)行SQL語句
var mysql = require("mysql");var connection = mysql.createConnection({ host : "localhost", // 你要連接的數(shù)據(jù)庫服務(wù)器的地址 port : 3306,// 端口號 user : "root", // 連接數(shù)據(jù)庫服務(wù)器需要的用戶名 password : "root", // 連接數(shù)據(jù)庫服務(wù)器需要的密碼 database : "gz61" //你要連接的數(shù)據(jù)庫的名字});connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});參考地址: www.npmjs.com/package/mys…
執(zhí)行查詢類型的SQL語句,查詢結(jié)果(result)是一個數(shù)組,每個單元是對象,對象的屬性是數(shù)據(jù)表的字段名。
// 1. 加載mysqlconst mysql = require("mysql");// 2. 創(chuàng)建連接對象const conn = mysql.createConnection({ // 對象的屬性名字不能改變 host: "localhost", port: 3306, user: "root", password: "root", database: "gz61"});// 3. 連接到MySQL服務(wù)器connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});// 4. 執(zhí)行SQL語句let sql = "select id,name,age from stu";connection.query(sql, (err, result, fields) => { if (err) throw err; // throw err 相當于 return console.log(err); console.log(result); // result就是查詢結(jié)果});執(zhí)行添加類型的SQL語句,查詢結(jié)果(result)是一個對象,該對象中有兩個屬性要關(guān)注:
affectedRows: 受影響行數(shù)insertID: 查詢數(shù)據(jù)的主鍵值// 1. 加載mysqlconst mysql = require("mysql");// 2. 創(chuàng)建連接對象const conn = mysql.createConnection({ // 對象的屬性名字不能改變 host: "localhost", port: 3306, user: "root", password: "root", database: "gz61"});// 3. 連接到MySQL服務(wù)器connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});let sql = "insert into users (name,password) values("小王","snv")"connection.query(sql, (err, result) => { if (result.affectedRows > 0) { console.log("添加成功,新數(shù)據(jù)的id為:" + result.insertId); } else { console.log("添加失敗"); }});執(zhí)行修改類型的SQL語句,查詢結(jié)果(result)是一個對象,該對象中有 affectedRows 屬性,表示本次修改操作影響到的行數(shù)。
// 1. 加載mysqlconst mysql = require("mysql");// 2. 創(chuàng)建連接對象const conn = mysql.createConnection({ // 對象的屬性名字不能改變 host: "localhost", port: 3306, user: "root", password: "root", database: "gz61"});// 3. 連接到MySQL服務(wù)器connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});// 更新// update stu set 字段=值,字段=值 where id=11let sql = "update users set password="123" where name="小王"";conn.query(sql, (err, result) => { if (err) throw err; if (result.affectedRows > 0) { console.log("修改成功"); } else { console.log("修改失敗"); }})執(zhí)行刪除類型的SQL語句,查詢結(jié)果(result)是一個對象,該對象中有 affectedRows 屬性
// 1. 加載mysqlconst mysql = require("mysql");// 2. 創(chuàng)建連接對象const conn = mysql.createConnection({ // 對象的屬性名字不能改變 host: "localhost", port: 3306, user: "root", password: "root", database: "gz61"});// 3. 連接到MySQL服務(wù)器connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});// 刪除let sql = "delete from stu where id=1";connection.query(sql,(err, result) => { if (err) throw err; if (result.affectedRows > 0) { console.log("刪除成功"); } else { console.log("刪除失敗"); }});做刪除 : delete from 表名 條件會把數(shù)據(jù)直接從數(shù)據(jù)庫中刪除掉!
思路:
不是真的刪除,而是設(shè)置一個特殊的字段表示當前的狀態(tài):正常還是已經(jīng)刪除# 目標:把id=16的軟刪除掉update stu set isDelete=1 where id=16
分析上面幾個單獨的功能點,它們基本的語法格式是一致的,只是要執(zhí)行的sql語句不同而已,所以,我們可以對它們進行一個簡單的封裝。然后再寫測試文件對其進行測試。
涉及兩個文件:
sql.jssqltest.js模塊名:sql.js
// 由于四項(insert,delete,update,select)操作只是sql語句不同// 1. 加載mysqlconst mysql = require("mysql");// 2. 創(chuàng)建連接對象const conn = mysql.createConnection({ // 對象的屬性名字不能改變 host: "localhost", port: 3306, user: "root", password: "root", database: "gz61"});// 3. 連接到MySQL服務(wù)器connection.connect((err) => { // 如果有錯誤對象,表示連接失敗 if (err) return console.log("數(shù)據(jù)庫連接失敗") // 沒有錯誤對象提示連接成功 console.log("mysql數(shù)據(jù)庫連接成功")});module.exports = connectionsqltest.js
const conn = require("./sql");conn.query("select * from users where username="小美1" and userpassword="666"", (err, data) => { console.log(err); console.log(data); if (data.length > 0) { console.log("用戶名密碼Ok"); } else { console.log("用戶名密碼error"); }});更多node相關(guān)知識,請訪問:nodejs 教程!
以上就是聊聊node怎么操作MySQL數(shù)據(jù)庫(增刪改查)的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!