码迷,mamicode.com
首页 > 数据库 > 详细

Node.js 数据库

时间:2017-08-14 14:26:50      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:pen   mod   gif   lex   ase   log   连接数据库   sites   websites   

通过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);
})
View Code

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(‘------------------------------------------------------------\n\n‘); 
});
connection.end();
View Code

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(‘-----------------------------------------------------------------\n\n‘); 
});

connection.end();
View Code

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(‘-----------------------------------------------------------------\n\n‘); 
});

connection.end();
View Code

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(‘-----------------------------------------------------------------\n\n‘);
});
connection.end();
View Code

 

Node.js 数据库

标签:pen   mod   gif   lex   ase   log   连接数据库   sites   websites   

原文地址:http://www.cnblogs.com/starryxsky/p/7357343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!