数据库
- 安装mysql两种方法(推荐第二种)
- 安装wamp 开启 mysql服务
- 安装mysql 开启服务
- 库操作
- 安装mysql中间件
npm i mysql -D 连接数据库(推荐方法2)
123456789101112131415161718192021222324252627282930313233//方法一:let mysql = require('mysql');let db = mysql.createConnection({truehost:'localhost',//主机名trueuser:'root',//mysql账号truepassword:'123456',//mysql密码truedatabase:'test'//mysql数据库});db.connect();//连接数据库let str = 'select * from user';let str1 = 'insert into user values(default,"tbl",123)';//执行sql语句db.query(str1,(err,data)=>{trueconsole.log(err);trueconsole.log(data);})//方法2//创建库连接池let mysql = require('mysql');let db=mysql.createPool({host:'localhost',user:'root',password:'root123',database:'2017-8-29',port:3306});let str = 'select * from user';let str1 = 'insert into user values(default,"tbl",123)';//执行sql语句db.query(str1,(err,data)=>{trueconsole.log(err);trueconsole.log(data);})mysql中间件API
- 执行sql语句
db.query(sql语句,回调(err,data));- err 返回错误信息
- data 数据库数据(array)
- 执行sql语句
mysql语句
增:
INSERT INTO 表(字段列表) VALUES (值列表)
INSERT INTO user(ID,username,password) VALUES(0,"ppp","ppp123")
删:
DELETE FROM 表 WHERE 字段名=值
DELETE FROM user WHERE username="ppp"
改:
UPDATE 表 SET 字段名=值 WHERE 字段名=值
UPDATE user SET username="uncle9" WHERE username="alex"
查:
SELECT ? FROM 表
? == 字段名 / 字段1,字段2 / *
SELECT * FROM user
SELECT username,password FROM user