Learning MySQL
MySQL安裝教程:
1.下載壓縮包
1.1 開啟下載域名: https://dev.mysql.com/downloads/
1.2 選擇MySQL Community Server進入
1.3 選擇 Other Downloads: => 411.8M (實際情況根據版本變化)
2.安裝MySQL
2.1 解壓後雙擊執行等待
2.2 選擇Server only => Next
2.3 選擇Execute => 等待安裝 => Next(一直點)
2.4 Next
2.5 Next
2.6 修改為第二個選項 ( 傳統密碼驗證 )
2.7 設定MySQL最高許可權密碼- -請牢記(使用者名稱預設:root)
2.8 Next => Next => Execute(等待) => Finish => Next => Finish
2.9 在最近新增中開啟選中內容
2.10 輸入密碼 => 安裝成功
Navicat- MySQL視覺化工具安裝教程(可選):
請參考文章: https://www.cnblogs.com/yinfei/p/11427259.html
Node連線MySQL:
進入node專案根目錄執行
npm install mysql -S
新建test.js
const mysql = require("mysql")
const options = {
host:"localhost",
user:"root",
password:"*******",
database:"test",
}
const con = mysql.createConnection(options)
con.connect((err)=>{
if(err) console.log(err)
else{console.log("資料庫連線成功")}
})
const strSql = "select * from student"
con.query(strSql,(err,results,fields)=>{
if(err) return console.log(err)
console.log(results)
console.log(fields)
})
const strSql1 = "drop table user"
con.query(strSql1,(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
const strSql2 = "drop database shop"
con.query(strSql2,(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
const strSql3 = "create database shop"
con.query(strSql3,(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
let strSql4 = `
create table user
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);`
con.query(strSql4,(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
let strSql5="insert into user(id,name,sex,birthday,entry_date,salary,resume) values(2,'yj','man','1998-01-04','2020-7-11','1000','i am a developer');"
con.query(strSql5,(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
let strSql6="insert into user(name,sex,birthday,entry_date,salary,resume) values(?,?,?,?,?,?);"
con.query(strSql6,['yj','man','1998-01-04','2020-7-11','1000','i am a developer'],(err,results)=>{
if(err) return console.log(err)
console.log(results)
})
var delSql = 'delete from user where id=2';
con.query(delSql, function (err, res) {
if(err) return console.log(err)
console.log("資料庫刪的結果:");
console.log(res);
});
var modSql = 'update user set name=?,birthday=? where id=?';
var modSqlParams = ['cyj', '1998-01-04', 1];
con.query(modSql, modSqlParams, function (err, res) {
if(err) return console.log(err)
console.log("資料庫更改的結果:");
console.log(res);
});
期待與小夥伴們的下次見面...