通过Node.js语言连接数据库,通过Express框架指明数据库连接的位置。
一、Express 框架下Node.js连接数据库的写法
在同一个目录下写setting.js和connet.js,分别负责数据库的设置和连接
setting.js
module.exports = { host:'……', user:'root', password:'123456', port:3306, database:'数据库名称' };
connect.js
var settings = require('./settings'); var mysql = require('mysql'); var client = mysql.createConnection(settings, function(err, stream) { }); client.connect(function(err) { if (err) { console.log('error connecting: ' + err.stack); throw err; } console.log('connect as id' + client.threadId); }) client.on('error', function() {}); module.exports = client;
express框架下数据库模块的加载
let db = require('./database/querydb.js'); app.use(express.static('./public')); app.use(express.static('./database')); app.use(express.static('./build'))
二、单纯的Node.js数据库连接与操作
数据库连接:
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'transport', }); connection.connect(function(err) { if (err) { console.log('error connecting: ' + err.stack); throw err; } console.log('connect as id' + client.threadId); })
select操作
var sql = 'SELECT * FROM websites'; //查 connection.query(sql,function (err, result) { if(err){ console.log('[SELECT ERROR] - ',err.message); return; } console.log('--------------------------SELECT----------------------------'); console.log(result); console.log('------------------------------------------------------------ '); }); connection.end();
delete操作
var delSql = 'DELETE FROM websites where id=6'; //删 connection.query(delSql,function (err, result) { if(err){ console.log('[DELETE ERROR] - ',err.message); return; } console.log('--------------------------DELETE----------------------------'); console.log('DELETE affectedRows',result.affectedRows); console.log('----------------------------------------------------------------- '); }); connection.end();
insert操作
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'test', }); connection.connect(); var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)'; var addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453', 'CN']; //增 connection.query(addSql,addSqlParams,function (err, result) { if(err){ console.log('[INSERT ERROR] - ',err.message); return; } console.log('--------------------------INSERT----------------------------'); //console.log('INSERT ID:',result.insertId); console.log('INSERT ID:',result); console.log('----------------------------------------------------------------- '); }); connection.end();
update操作
var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?'; var modSqlParams = ['菜鸟移动站', 'https://m.runoob.com',6]; //改 connection.query(modSql,modSqlParams,function (err, result) { if(err){ console.log('[UPDATE ERROR] - ',err.message); return; } console.log('--------------------------UPDATE----------------------------'); console.log('UPDATE affectedRows',result.affectedRows); console.log('----------------------------------------------------------------- '); }); connection.end();