Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
write data to DB/2
var db = require('/QOpenSys/QIBM/ProdData/Node/os400/db2i/lib/db2');
var chatdata = {
date : "2015-01-04",
time : "10:45:05",
user : "Rainer",
text : "this is a wonderful text"
};
function createString(data) {
data.time = getTime();
var keys = Object.keys(data);
var values = new Array(keys.length);
for (var i = 0; i < keys.length; i++) {
values[i] = "'" + data[keys[i]] + "'";
}
return values;
}
function getTime() {
var date = new Date();
return date.getHours().toString().lpad("0", 2) + ":"
+ date.getMinutes().toString().lpad("0", 2) + ":"
+ date.getSeconds().toString().lpad("0", 2);
}
String.prototype.lpad = function(padString, length) {
var string = this;
while (string.length < length) {
string = padString + string;
}
return string;
};
function writeData(data) {
var sql = "INSERT INTO TSTO.CHATLOGP (CHATDATE, CHATTIME, CHATUSER, CHATTEXT) "
+ "VALUES (" + createString(data) + ")";
console.log("Sql: " + sql);
db.init();
db.conn("*LOCAL");
db.autoCommit(true);
db.exec(sql);
db.close;
}
writeData(chatdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment