您好任何人都可以举例说明如何在nodejs中使用insert语句。我能够使用选择查询。但对于插入查询,我得到的结果为[]。没有错误可以看到,但值不会被添加到原始表。我正在使用db2,ibm_db,express,nodejs和angularjs。如何使用节点js将数据插入到db2(ibm_db)
0
A
回答
1
前段时间我写了一个blog entry on using DB2 and node.js on Bluemix。它包含一个INSERT语句的代码。
作为插入件的一部分
- 首先准备语句,
- 然后结合到被插入的值和
- 最后执行该语句。
下面是相关代码片段中,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的成员之一)跟随节点IBM_DB的github仓库(https://github.com/ibmdb/node-ibm_db) ,我们已经更新了README文档以及执行特定任务的API列表。
对于上述查询,您可以使用“.prepare(sql,callback)”或“.prepareSync(sql)”API(根据您的要求进行异步/同步调用),下面是附加的代码片段和URL链接特定的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的网址):https://github.com/ibmdb/node-ibm_db#-8-preparesql-callback
相关问题
- 1. 如何使用插入多条记录到Oracle数据库节点JS
- 2. 使用SAS将记录插入到DB2数据库中
- 3. 插入NULL - 节点Js // MongoDB
- 4. 插入JSON到MySQL表中节点JS
- 5. 如何将Object的数组插入到带有节点js的mysql中?
- 6. 如何在早期版本的DB2中使用IBM_DB?
- 7. 如何从JS发送阵列数据使用节点JS
- 8. 如何将json输出插入mysql与节点js
- 9. 如何使用linq2xml插入xml节点?
- 10. 插入在DB2数据库
- 11. 了解D3.js如何将数据绑定到节点
- 12. 使用节点JS到MongoDB的使用节点JS
- 13. 如何使用角度js和节点js将数组POST到MongoDB
- 14. 使用节点JS获取API数据
- 15. 如何发送节点js服务器端数据到角js
- 16. 如何使用Asp.net将数据插入到MySQL数据库
- 17. 如何使用dom4j在其他节点之前插入节点?
- 18. 节点Js Async for循环并在mongo中插入数据
- 19. 如何使用nodejs和ibm_db包从DB2中的表中获取最后一个插入ID
- 20. Django,Informix,ibm_db和DB2数据服务器驱动程序
- 21. 将节点插入到双链表中
- 22. SSIS-DB2:将数据更改值插入到表中时
- 23. 节点js Postgresql - 错误时插入表
- 24. 从节点js在cassandra中插入BigInt
- 25. 节点JS插入Mysql的不工作
- 26. 节点js mongo查找最后插入
- 27. MySQL插入不工作在节点js
- 28. 使用Ajax将数据插入到mysql
- 29. 使用get导入对象数据节点js
- 30. 如何使用节点js同步读取sqlite3数据库?
罗希特感谢您的帮助。通过你提到的链接和其他几个链接帮助我 –