MysqL使用
安装MysqL 模块: 在安装根目录 cmd命令行执行命令安装成功后、 MysqL数据库表 已存在的情况下。 在nodejs根目录 新建MysqL.js:
"); response.write("已经连接上MysqL....
"); } clientReady(client); }); };
clientReady = function(client) {
var values = ['不错啊'];
client.query('insert into nodeMysqL set names = :1',values,results) {
if(error) {
console.log("ClientReady Error: " + error.message);
client.end();
return;
}
console.log('Inserted: ' + results.affectedRows + ' row.');
console.log('Id inserted: ' + results.insertId);
}
);
getData(client);
}
getData = function(client) {
client.query(
'select * from nodeMysqL',function selectCb(error,results,fields) {
if (error) {
console.log('GetData Error: ' + error.message);
client.end();
return;
}
var data = '';
for(var i=0; i<results.length; i++){
var firstResult = results[i];
data += 'id: ' + firstResult['id']+' name: ' + firstResult['names']+"
";
}
response.write(data);
response.write("<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a><a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>连接...");
response.write("</html>");
response.end();
}
);
client.end();
};
clientConnectionReady(client);
});
server.listen(8033,"127.0.0.1");
var sys = require("util");
sys.puts("Server running at http://localhost:8033/");
运行 node MysqL.js 。 浏览器 访问 http://localhost:8033 即可看到效果。
配置oracle支持
在oracle网站下载oracle数据库客户端连接包 instantclient-basic-linux,instantclient-sdk-linux 解压oracle客户端连接模块$ sudo ln -s libocci.so.11.1 libocci.so
$ sudo ln -s libclntsh.so.11.1 libclntsh.so
配置环境变量
进入nodejs目录 安装oracle模块支持
export LD_LIBRARY_PATH=/opt/instantclient
编写oracle.js文件 测试连接于执行sql是否正常
<div class="jb51code">
<pre class="brush:js;">
var oracle = require("oracle");
oracle.connect({ "hostname": "localhost","user": "demo","password": "demo","database": "orcl","port": 1521},function(err,connection) {
if(err) {
console.log(err);
}
// selecting rows 注意 connection.execute 方法必须要三个参数 不然会出错
connection.execute("SELECT * FROM TEST WHERE ID = :1",['1'],function(err1,results) {
// results will be an array of objects
console.log("query start");
if(err1) {
console.log(err1);
}
// console.log(results.length);
for(var i = 0; i < results.length; i++) {
console.log(results[i].ID);
}
connection.close();
});
});