const mysql = require('mysql2') //注意是mysql2,不是mysql。mysql2支持mysql8.0以上的加密方式
const db = mysql.createPool({
host : '127.0.0.1',
user : 'root',
password : 'root',
database : 'study'
})
const sqlStr2 = 'insert into users (name, age) values (?, ?)' //普通增加
db.query(sqlStr2, ['jsc', '30'], (err, results)=>{
if(err) return console.log(err.message)
if(results.affectedRows === 1) console.log('insert ok')
})
const zhengge={'name':'zhengge', 'age':16, 'occupation':'singer'} //以对象方式增加
db.query('insert into users set ?', zhengge, (err, result)=>{
if(err) return console.log(err.message)
console.log('zhengge has been added.')
})
const sqlStr3 = 'delete from users where id>=4' //删
db.query(sqlStr3, (err, result)=>{
if(err) return console.log(err.message)
console.log(result.affectedRows + ' lines are deleted')
})
db.query('update users set name="lovely xc" where name="xc"',(err, result)=>{省略}) //改
const sqlStr = 'select * from users' //查
db.query(sqlStr,(err, result)=>{
if(err) return console.log(err.message)
console.log(result)
})
标签:console,log,err,db,Express,改查,result,Mysql,const
From: https://www.cnblogs.com/xlege/p/16748401.html