安装mysql
模块:
npm install mysql
初始化并连接mysql
var mysql = require("mysql"); var conn = mysql.createConnection({ host: 'localhost', port: 3306, user: 'root', password: 'root', database: 'mysql' }); conn.connect();
执行SQL
语句并获取结果
conn.query("SELECT user,host,password FROM user", function(err, rows, fields){ if (err) throw err; for (var i in rows){ console.log("[%s] USER:%s HOST:%s PASS:%s", i, rows[i].user, rows[i].host, rows[i].password); } });
The End.
暂时就写到这了,用到的时候再查询记录吧!具体更多详细功能可参考:用Nodejs连接MySQL