Node连接MySQL数据库

梦想的初衷 提交于 2019-12-14 05:42:14

使用 Node 操作 MySQL 数据库

安装:

npm install --save mysql

案例:

var mysql = require('mysql');

// 1. 创建连接
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'users'
});
 
// 2. 连接数据库
connection.connect();
 
// 3. 执行数据操作
connection.query('select * from users', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results);
});


connection.query('insert into users values(null, "admin", "123456")', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results);
});
 
// 4. 关闭连接
connection.end();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!