操作のMysqlの簡単な例をNode.jsの

APIパッケージ:システムに利用可能なツールのパッケージは、データベースには、行とに分かれています。
用途:APIは、データベースの直接操作は、もはや偽のデータを使用していることができません。

DEMOコード:

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();

おすすめ

転載: www.cnblogs.com/wljqds/p/11579007.html