Node接入Mysql步骤
1、本地下载mysql服务
2、用GUI 客户端(Workbench、navicat)创建数据库,建表。
3、node 里面需要用一个驱动mysqp2连接数据库。
// get the clientconst mysql = require('mysql2');// create the connection to databaseconst connection = mysql.createConnection({host: 'localhost',user: 'root',database: 'test'});// simple queryconnection.query('SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',function(err, results, fields) {console.log(results); // results contains rows returned by serverconsole.log(fields); // fields contains extra meta data about results, if available});// with placeholderconnection.query('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',['Page', 45],function(err, results) {console.log(results);})
