标签:pen mod gif lex ase log 连接数据库 sites websites
通过Node.js语言连接数据库,通过Express框架指明数据库连接的位置。
在同一个目录下写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‘))
数据库连接:
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(‘------------------------------------------------------------\n\n‘); }); 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(‘-----------------------------------------------------------------\n\n‘); }); 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(‘-----------------------------------------------------------------\n\n‘); }); 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(‘-----------------------------------------------------------------\n\n‘); }); connection.end();
标签:pen mod gif lex ase log 连接数据库 sites websites
原文地址:http://www.cnblogs.com/starryxsky/p/7357343.html