[英]How to insert data to db2 using node js ( ibm_db)
Hi Can anyone give an example of how use insert statement in nodejs. 嗨,谁能举一个例子说明如何在nodejs中使用insert语句。 I am able to use select query. 我可以使用选择查询。 But for insert query i am getting the result as []. 但是对于插入查询,我得到的结果为[]。 no error can be seen but the values are not added to the original table. 没有看到错误,但这些值未添加到原始表中。 I am using db2, ibm_db,express,nodejs and angularjs. 我正在使用db2,ibm_db,express,nodejs和angularjs。
I wrote a blog entry on using DB2 and node.js on Bluemix a while ago. 不久前,我写了一篇关于在Bluemix上使用DB2和node.js的博客条目 。 It includes code for an INSERT statement. 它包含INSERT语句的代码。
As part of the insert 作为插入的一部分
Here is the relevant code snippet, the full context is in the blog : 这是相关的代码段, 完整的上下文在博客中 :
exports.insertIP = function(ibmdb,connString,ipinfo) {
console.log("insertIP called",ipinfo);
ibmdb.open(connString, function(err, conn) {
if (err ) {
res.send("error occurred " + err.message);
}
else {
// prepare the SQL statement
conn.prepare("INSERT INTO IP.VISITORS(vtime,ip,country_code,country,region_code,region,city,zip,latitude,longitude,metro,area) VALUES (current timestamp,?,?,?,?,?,?,?,?,?,?,?)", function(err, stmt) {
if (err) {
//could not prepare for some reason
console.log(err);
return conn.closeSync();
}
//Bind and Execute the statment asynchronously
stmt.execute([ipinfo["ip"],ipinfo["country_code"],ipinfo["country_name"],ipinfo["region_code"],ipinfo["region_name"],ipinfo["city"],ipinfo["zipcode"], ipinfo["latitude"], ipinfo["longitude"],ipinfo["metro_code"],ipinfo["area_code"]], function (err, result) {
console.log(err);
// Close the connection to the database
conn.close(function(){
console.log("Connection Closed");
});
});
});
}
})};
I would suggest and recommend (as one of the members of node-ibm_db) to follow the node-ibm_db github repository ( https://github.com/ibmdb/node-ibm_db ) , we have updated the README document as well as the list of APIs to do particular tasks. 我建议并推荐(作为node-ibm_db的成员之一)遵循node-ibm_db github存储库( https://github.com/ibmdb/node-ibm_db ),我们已经更新了README文档以及执行特定任务的API列表。
For your above query you can use ".prepare(sql, callback)" or ".prepareSync(sql)" API (as per your requirements Async/sync call), below is the attached code snippet and URL link for particular API documentation. 对于上述查询,您可以使用“ .prepare(sql,callback)”或“ .prepareSync(sql)” API(根据您的要求Async / sync调用),以下是特定API文档的随附代码段和URL链接。
var ibmdb = require("ibm_db"),
cn ="DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx";
ibmdb.open(cn,function(err,conn){
conn.prepare("insert into hits (col1, col2) VALUES (?, ?)",
function (err, stmt) {
if (err) {
//could not prepare for some reason
console.log(err);
return conn.closeSync();
}
//Bind and Execute the statment asynchronously
stmt.execute(['something', 42], function (err, result) {
if( err ) console.log(err);
else result.closeSync();
//Close the connection
conn.close(function(err){});
});
});
});
API documentation(Github URL) : https://github.com/ibmdb/node-ibm_db#-8-preparesql-callback API文档(Github URL): https : //github.com/ibmdb/node-ibm_db#-8-preparesql-callback
Try to install jt400 by using the below command npm install node-jt400 --save 尝试使用以下命令安装jt400 npm install node-jt400 --save
use the below code to insert the data to table name foo. 使用以下代码将数据插入表名称foo。 Follow link https://www.npmjs.com/package/node-jt400 for for details 有关详细信息,请访问链接https://www.npmjs.com/package/node-jt400
pool
.insertAndGetId('INSERT INTO foo (bar, baz) VALUES(?,?)',[2,'b'])
.then(id => {
console.log('Inserted new row with id ' + id);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.