I am working with node js and postgresql. I tried to insert multiple query at a time.
ArrayList:
var keywordsData = [ { name: 'demo', count: 69 }, { name: 'healthy', count: '22' }, { name: 'cooking', count: '12' }, { name: 'food', count: '9' }, { name: 'home', count: '9' }, { name: 'organic', count: '7' }, { name: 'live', count: '6' }, { name: 'openmrs', count: '6' }];
Javascript code:
for (var indexs in keywordsData) {
var item = keywordsData[indexs].name;
var count = keywordsData[indexs].count;
var goalId = 10;
console.log("first" + indexs);
if (item.length > 1) {
client.query("insert into real_keywords(reference_id,keyword,keyword_count) values('" + goalId + "','" + item + "','" + count + "')", function(err, result) {
console.log("last" + indexs);
});
}
}
Output i am getting like this:
first0
first1
first2
first3
first4
first5
first6
first7
last7
last7
last7
last7
last7
last7
last7
last7
Expected output:
first0
last0
first1
last1
first2
last2
first3
last3
first4
last4
first5
last5
first6
last6
first7
last7
Please anyone can suggest the best way to solve this.
indexesis, but you can insert all your rows at once. stackoverflow.com/questions/6889065/… 2) You should consider using a prepared statement instead of string concatenation. postgresql.org/docs/9.2/static/sql-prepare.html