您好,登錄后才能下訂單哦!
使用Nodejs怎么操作postgresql?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
PG.js文件如下:
var pg = require('pg'); var conString = "postgres://username:password@localhost/databasename"; var client = new pg.Client(conString); var PG = function(){ console.log("準(zhǔn)備向****數(shù)據(jù)庫連接..."); }; PG.prototype.getConnection = function(){ client.connect(function (err) { if (err) { return console.error('could not connect to postgres', err); } client.query('SELECT NOW() AS "theTime"', function (err, result) { if (err) { return console.error('error running query', err); } console.log("hbdfxt數(shù)據(jù)庫連接成功..."); }); }); }; // 查詢函數(shù) //@param str 查詢語句 //@param value 相關(guān)值 //@param cb 回調(diào)函數(shù) var clientHelper = function(str,value,cb){ client.query(str,value,function(err,result){ if(err) { cb("err"); } else{ if(result.rows != undefined) cb(result.rows); else cb(); } }); } //增 //@param tablename 數(shù)據(jù)表名稱 //@param fields 更新的字段和值,json格式 //@param cb 回調(diào)函數(shù) PG.prototype.save = function(tablename,fields,cb){ if(!tablename) return; var str = "insert into "+tablename+"("; var field = []; var value = []; var num = []; var count = 0; for(var i in fields){ count++; field.push(i); value.push(fields[i]); num.push("$"+count); } str += field.join(",") +") values("+num.join(",")+")"; clientHelper(str,value,cb); }; //刪除 //@param tablename 數(shù)據(jù)表名稱 //@param fields 條件字段和值,json格式 //@param cb 回調(diào)函數(shù) PG.prototype.remove = function(tablename,fields,cb){ if(!tablename) return; var str = "delete from "+tablename+" where "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$" +count); value.push(fields[i]); } str += field.join(" and "); clientHelper(str,value,cb); } //修改 //@param tablename 數(shù)據(jù)表名稱 //@param fields 更新的字段和值,json格式 //@param mainfields 條件字段和值,json格式 PG.prototype.update = function(tablename,mainfields,fields,cb){ if(!tablename) return; var str = "update "+tablename+" set "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$"+count); value.push(fields[i]); } str += field.join(",") +" where "; field = []; for(var j in mainfields){ count++; field.push(j+"=$"+count); value.push(mainfields[j]); } str += field.join(" and "); clientHelper(str,value,cb); } //查詢 //@param tablename 數(shù)據(jù)表名稱 //@param fields 條件字段和值,json格式 //@param returnfields 返回字段 //@param cb 回調(diào)函數(shù) PG.prototype.select = function(tablename,fields,returnfields,cb){ if(!tablename) return; var returnStr = ""; if(returnfields.length == 0) returnStr = '*'; else returnStr= returnfields.join(","); var str = "select "+returnStr+ " from "+tablename+" where "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$"+count); value.push(fields[i]); } str += field.join(" and "); clientHelper(str,value,cb); }; module.exports = new PG();
用法很簡單,如下:
var pgclient = require('./PG');// 引用上述文件 pgclient.getConnection(); // 調(diào)用上述四個函數(shù)即可 pgclient.save('userinfo',{'name': admin},cb);<span >.</span>
關(guān)于使用Nodejs怎么操作postgresql問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。