NodeJs链接Mysql数据库

安装node-mysql:
npm install mysql@2.0.0-alpha9

npm install node-mysql

node程序启动文件app.js:

var express = require(‘express‘);
var mysql = require(‘mysql‘);
var app = express();

app.use(function(req, res, next){
  console.log(‘%s %s‘, req.method, req.url);
  next();
});

var conn = mysql.createConnection({
    host:‘localhost‘,
    user:‘root‘,
    database:‘nodedb‘,
    password:‘********‘,
    port:3306
});

conn.connect();
app.get(‘/‘, function(req, res){

    conn.query(‘SELECT * from news‘, function(err, rows, fields) {
        if (err) throw err;
        var data = ‘‘;
        for(var i = 0; i < rows.length; i++){
            data += ‘<p>‘ + ‘ID:‘ + rows[i].id + ‘</p>‘;
            data += ‘<p>‘ + ‘title:‘ + rows[i].title + ‘</p>‘;
            data += ‘<p>‘ + ‘contents:‘ + rows[i].contents + ‘</p>‘;
            data += ‘<hr />‘;
        }
        res.send(data);

    });
});

app.listen(3000);

console.log(‘Listening on port 3000‘);



NodeJs链接Mysql数据库,布布扣,bubuko.com

NodeJs链接Mysql数据库

上一篇:SQLServer数据迁移自增列IDENTITY


下一篇:Poco数据库操作