/**
* 使用node连接mysql模块
*/
//引入MySQL数据库
const mysql = require("mysql");
//创建一个mysql连接对象,连接是使用对象来连接数据库
let connection = mysql.createConnection({
host:"127.0.0.1",
post:"3306",
user:"root",
password:"123",
database:"school"
});
//建立连接
connection.connect();
//执行删除操作
/* connection.query("delete from student where sid = 8",(err,result)=>{
if(err)
throw err;
//affectedRows为受影响的行数
console.log("删除成功!");
}); */
//执行修改操作
/* connection.query("update student set ssex = '女' where sid = 9",(err,result)=>{
if(err)
throw err;
console.log("修改成功!");
}); */
//执行查询操作
// connection.query("select * from student",(err,result)=>{
// if(err)
// throw err;
// console.log(result);
// })
//执行插入数据
// connection.query("insert into student values(null,'渊哥','18','男')",(err,result)=>{
// console.log(result);
// })
//防止SQL注入
// connection.query("insert into student values(null,?,?,?)",['亮哥',22,'男'],(err,result)=>{
// console.log(result);
// })
//插入对象
let student = {
sid:'null',
sname:'小刘',
sage:'23',
ssex:'男'
}
connection.query('insert into student set ?',[student],(err,result)=>{
if(err)
throw err;
console.log(result);
})
connection.end();//关闭连接