Reputation: 68
I'm creating a MySQL database and I want to insert about 1000 records that are structured in a JSON array
This is the JSON array that I'm importing:
[
{
"DESCRIPTION": "انفرتر 1 حصان أحادي",
"TYPE": "ODE-2-12075-1K012XX",
"CLASSIFICATION": "VFD",
"BRAND": "INVERTEK",
"ORIGIN": "UK",
"FAMILY": "ODE-2",
"ar_classification": "انفرتر"
},
{
"DESCRIPTION": "انفرتر 2 حصان أحادي",
"TYPE": "ODE-2-120150-1K012XX",
"CLASSIFICATION": "VFD",
"BRAND": "INVERTEK",
"ORIGIN": "UK",
"FAMILY": "ODE-2",
"ar_classification": "انفرتر"
},
{
"DESCRIPTION": "انفرتر كتيم 3 حصان أحادي بدون\nمقاومة ومفتاح",
"TYPE": "ODE-2-24400-3K04N-XX",
"CLASSIFICATION": "VFD IP55",
"BRAND": "INVERTEK",
"ORIGIN": "UK",
"FAMILY": "ODE-2",
"ar_classification": "انفرتر"
}
]
const {readFileSync} = require('fs')
//Here is where the Json array is located
const first = readFileSync('./Zahabi-Products.json', 'utf8')
const products = JSON.parse(first)
pool.query("INSERT INTO products SET ?", products,
function(err, result) {
if(err) throw err;
console.log('data inserted');
});
But the result is that only the first object in the array is being inserted
How could I insert all of the array
Upvotes: 0
Views: 186
Reputation: 68
I tried this and it worked, but I think that is not the perfect answer
var test02 = {}
for(var i = 0; i <= products.length; i++ ){
test02 = products[i]
pool.query("INSERT INTO products SET ?",test02 ,
function(err, result) {
if(err) throw err;
console.log('data inserted');
});
}
Upvotes: 1