2016-12-16 15 views
0

こんにちは誰もnodejsでinsert文を使用する方法の例を挙げることができます。私は選択クエリを使用することができます。しかし、挿入クエリの結果は[]となっています。エラーは表示されませんが、値は元の表に追加されません。私は、db2、ibm_db、express、nodejs、およびangularjsを使用しています。ノードjs(ibm_db)を使用してdb2にデータを挿入する方法

答えて

1

私はblog entry on using DB2 and node.js on Bluemixを書きました。 INSERT文のコードが含まれています。

インサート

  1. の一部として最初
  2. 次いで文を実行最終的に値が挿入されるように結合し、
  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"); 
        }); 
        }); 
       }); 
       } 
      })}; 
1

私が提案すると、ノードのIBM_DB githubのリポジトリに従うこと(ノード-IBM_DBのメンバーの一人として)をお勧めします(https://github.com/ibmdb/node-ibm_db) READMEドキュメントと特定のタスクを実行するためのAPIのリストを更新しました。

上記のクエリでは、「.prepare(sql、callback)」または「.prepareSync(sql)」APIを使用できます(要件の非同期/同期呼び出しに準拠します)。特定のAPIドキュメント。

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ドキュメント(のGitHub URL):https://github.com/ibmdb/node-ibm_db#-8-preparesql-callback

+0

感謝のRohitあなたの助けのために。あなたが言及したリンクを通って行くと他のリンクが私を助けた –

関連する問題