node.js - 如何使用 Node js ( ibm_db ) 将数据插入到 db2

标签 node.js express db2 ibm-cloud

嗨,谁能举一个如何在nodejs中使用insert语句的例子。我可以使用选择查询。但对于插入查询,我得到的结果为[]。没有看到错误,但值没有添加到原始表中。我正在使用 db2、ibm_db、express、nodejs 和 angularjs。

最佳答案

我写了一个blog entry on using DB2 and node.js on Bluemix不久以前。它包括 INSERT 语句的代码。

作为插入内容的一部分

  1. 首先准备声明,
  2. 然后绑定(bind)要插入的值并
  3. 最终执行该语句。

这是相关的代码片段,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");  
                   });  
                  });  
                });  
              }  
          })};  

关于node.js - 如何使用 Node js ( ibm_db ) 将数据插入到 db2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177691/

相关文章:

java - 将数据追加到 DB2 blob

node.js - 将原始 http 响应转换为 Koa 兼容对象?

javascript - Node.js/Socket.io 使用安全连接在 IE 中不返回结果

node.js - 使用 MEAN 堆栈的动态页面标题 - Jade 和 Angular

sql - DB2 中的查询计数

database - DB2:从表中清除大量记录

node.js - 在 Electron 应用程序中启用硬件加速

node.js - 如何在 Node 中解析数据 URL?

javascript - 无法永远卸载nodejs

javascript - ExpressJS Multer 文件权限