一、下载MySQL
1.下载MySQL 不登陆,开始下载 2.安装教程
3.基本使用 ?(1)登录账户 ?(2)建库和表
二、mysql模块
操作mysql数据库(增删改查)
import mysql from 'mysql'
const db = mysql.createPool({
host: '127.0.0.1',
user: 'root',
password: '******',
database: 'my_db_01',
})
db.query('select 1', (err, results) => {
if (err) return console.log(err.message)
else console.log(results)
})
const sqlStr = 'select * from users'
db.query(sqlStr, (err, results) => {
if (err) return console.log(err.message)
console.log(results)
})
const user = { username: 'Spider-Man', password: 'pcc123' }
const sqlStr = 'insert into users (username, password) values (?, ?)'
db.query(sqlStr, [user.username, user.password], (err, results) => {
if (err) return console.log(err.message)
if (results.affectedRows === 1)
console.log('插入数据成功!')
})
const user = { username: 'Spider-Man2', password: 'pcc4321' }
const sqlStr = 'insert into users set ?'
db.query(sqlStr, user, (err, results) => {
if (err) return console.log(err.message)
if (results.affectedRows === 1) {
console.log('插入数据成功')
}
})
const user = { id: 6, username: 'aaa', password: '000' }
const sqlStr = 'update users set username=?, password=? where id=?'
db.query(sqlStr, [user.username, user.password, user.id], (err, results) => {
if (err) return console.log(err.message)
if (results.affectedRows === 1) {
console.log('更新成功')
}
})
const user = { id: 6, username: 'aaaa', password: '0000' }
const sqlStr = 'update users set ? where id=?'
db.query(sqlStr, [user, user.id], (err, results) => {
if (err) return console.log(err.message)
if (results.affectedRows === 1) {
console.log('更新数据成功')
}
})
const sqlStr = 'delete from users where id=?'
db.query(sqlStr, 5, (err, results) => {
if (err) return console.log(err.message)
if (results.affectedRows === 1) {
console.log('删除数据成功')
}
})
|