I have these values from json file "histogram":{ "1":5708, "2":838, "3":1097, "4":2964, "5":26308 } i wanted to insert them in mysql in column A,B,C,D,E respectively along with other data too i.e Date in node.js. on console i got the output right as i parse and stringify json. but it gives error "Column count doesn't match value count at row 1". Any help will be highly appreciated. NOTE: I am new to node so my code wont be good enough.
var dbCon = require('./db');
////////// necessary node modules////////
var readline = require('readline');
var stream = require('stream');
const utf8 = require('utf8');
///////////creating file stream to read a file/////////
var fs = require('fs');
var data1 = '';
// Create a readable stream
var request = require("request");
var readerStream = fs.createReadStream('app.json');
// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');
var outstream = new stream();
//createInterface - read through the stream line by line and print out data from it
var r1 = readline.createInterface(readerStream, outstream);
var lineCount = 0;
r1.on('line', function (line) {
// increment line count
lineCount++;
data1 = JSON.parse(line);
saveRecord(data1);
});
function saveRecord(data1) {
var histogram=data1["histogram"] ;
var id=data1["id"] ;
var datetime = new Date();
var histogramstring= JSON.stringify(histogram) ;
var sqlq1 = "INSERT INTO wp_histo1(`id`,`A`,`B`,`C`,`D`,`E`,`Date`) VALUES (?,?,?)" ;
dbCon.query(sqlq1,[ id,histogramstring,datetime]);
console.log('data inserted');
}
console.log("Program Ended");
Here is my console output.
code: 'ER_WRONG_VALUE_COUNT_ON_ROW',
errno: 1136,
sqlMessage: "Column count doesn't match value count at row 1",
sqlState: '21S01',
index: 0,
sql: 'INSERT INTO wp_histo1(`id`,`A`,`B`,`C`,`D`,`E`,`Date`) VALUES
(\'03125\',\'{\\"1\\":5708,\\"2\\":838,\\"3\\":1097,\\"4\\":2964,\\"5\\":26308}\',\'2021-02-02 15:45:25.326\')'
}
On the other hand if i try this query
var sqlq1 = "INSERT INTO wp_histo1(`id`,`A`,`Date`) VALUES (?,?,?)" ;
dbCon.query(sqlq1,[ id,histogramstring,datetime]);
Full histogram array get inserted in to column A. But i wanted to split it in A,B,C,D,E column.