Node.js simple example of the operation Mysql

API packages: a package for the tools available to the system, the database is divided into lines and lines.
Use: API allows direct operation of the database is no longer used false data.

DEMO Code:

const mysql = require('mysql');

// 创建数据库连接
const con = mysql.createConnection({
  // 主机名/端口/数据库/用户名/密码
  host: 'localhost',
  port: '3306',
  database: 'myblog',
  user: 'root',
  password: '113647'
});

// 开始连接
con.connect();

// sql语句
// 查询:返回查询结果
const sql = 'select id,username from users';
// 更新:返回结果中看affectedRows和changedRows字段
// const sql = 'select id,username from users';
// 插入:返回结果中看insertedId字段

// 执行sql语句
con.query(sql, (err, result) => {
  if (err) {
    console.log(err);
    return;
  }
  console.log(result);
});

// 关闭连接,否则结束不了该进程
con.end();

Guess you like

Origin www.cnblogs.com/wljqds/p/11579007.html