简体   繁体   中英

How to insert data to db2 using node js ( ibm_db)

Hi Can anyone give an example of how use insert statement in nodejs. 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.

I wrote a blog entry on using DB2 and node.js on Bluemix a while ago. It includes code for an INSERT statement.

As part of the insert

  1. first prepare the statement,
  2. then bind the values to be inserted and
  3. finally execute the statement.

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.

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.

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

Try to install jt400 by using the below command npm install node-jt400 --save

use the below code to insert the data to table name foo. Follow link https://www.npmjs.com/package/node-jt400 for for details

pool
  .insertAndGetId('INSERT INTO foo (bar, baz) VALUES(?,?)',[2,'b'])
  .then(id => {
    console.log('Inserted new row with id ' + id);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM