mysql

数据库

  1. 安装mysql两种方法(推荐第二种)
    1. 安装wamp 开启 mysql服务
    2. 安装mysql 开启服务
  2. 库操作
    • 客户顿
      1. wamp客户端是phpadmin
      2. mysql 客户端是navicat(收费)
    • 服务端
      1. wamp里面mysql(C:\wamp\mysql)
      2. mysql官网刚下载mysql(C:\Program Files\MySQL)

        mysql中间件

  3. 安装mysql中间件
    npm i mysql -D
  4. 连接数据库(推荐方法2)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    //方法一:
    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);
    })
  5. mysql中间件API

    1. 执行sql语句
      db.query(sql语句,回调(err,data));
      • err 返回错误信息
      • data 数据库数据(array)

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
文章目录
  1. 1. 数据库
  2. 2. mysql中间件
    1. 2.1. mysql语句
|