const mysql = require('mysql'); //先安装mysql模块 npm install mysql
/*let db = mysql.createConnection({host:'localhost',user:'root',password:'admin',port:3306, database:'mydatabase'});*/
//一般使用连接池方式
let db = mysql.createPool({host:'localhost',user:'root',password:'admin',port:3306, database:'mydatabase'}); //maxConnection:'10' 此为隐藏参数。默认10个链接,一般够用了
db.query('select * from user',(err,data)=>{
if(err){
console.log('错了',err);
}
console.log(data);
});
SQL:
1.增 INSERT
INSERT INTO 表 (字段列表) VALUES(值列表)
INSERT INTO user_table (ID, name, gender, chinese, math, english) VALUES(0, 'blue', '男', 35, 18, 29);
2.删 DELETE
DELETE FROM 表 WHERE 条件
DELETE FROM user_table WHERE ID=3;
3.改 UPDATE
UPDATE 表 SET 字段=值, 字段2=值2, ... WHERE 条件
UPDATE user_table SET chinese=100 WHERE ID=2;
4.查 SELECT
SELECT 字段列表 FROM 表 WHERE 条件
SELECT name, gender FROM user_table WHERE ID=2;
网友评论