selects only return partial result - mysql

I have around 24k of rows in mysql database. Now using nodejs I would like to query and then store it in json file.
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '10.1.1.2',
user : 'xxx',
password : 'xxx',
database : 'xxx'
});
connection.connect();
var json = '';
var query = 'SELECT * FROM st_training GROUP BY EndDate ORDER BY StartDate DESC';
var query2 = 'SELECT * FROM st_training WHERE EmployeeID=901 GROUP BY EndDate ORDER BY StartDate DESC';
connection.query(query, function(err, results) {
if (err) { throw err;
}
else {
jsonV = JSON.stringify(results);
console.log(jsonV);
fs.writeFile('table.json', JSON.stringify(results), function (err) {
if (err) throw err;
console.log('Saved!');
});
}
connection.end();
});
I can verify only partial around 600 rows being saved from console.log(jsonV) output or in table.json file.
What could go wrong here? is it there is a max limitation for JSON.stringify ?

Related

Receive data from multiple tables [Nodejs,MySQL]

What do I need to do to get data from multiple tables?
var express = require('express');
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '',
user : '',
password : '',
database : ''
});
connection.connect(function(){
console.log("MySQL Database is Connected");
});
app.use(express.static(__dirname + '/css'));
app.use(express.static(__dirname + '/js'));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.get('/',function(req,res){
res.render('index.html');
});
app.get('/load',function(req,res){
connection.query("select * from terms WHERE status = 1",
function(err,rows,fields){
if(err) throw err;
res.end(JSON.stringify(rows));
});
});
app.listen(7001,function(){
console.log("App is started at PORT 7001");
});
With this I can only get data from the terms table. But I need to get data from the impTerms table.
How do I get this?
Thank you
Use sql join in query , has nothing to do with node js.
con.connect(function(err) {
if (err) throw err;
var sql = "SELECT terms.id, terms.name FROM terms JOIN impTerms ON impTerms.id= terms.id and terms.status=1";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
There are several ways you can do,
Passing two sql queries to connection
Var sqlQuery = "select * from terms WHERE status = 1;select * from impTerms";
connection.query(sqlQuery,
function(err,rows,fields){
if(err) throw err;
res.end(JSON.stringify(rows));
});
Output:
[
[], // array of object for terms
[] // array of object for impTerms
]
Changing select query
Var sqlQuery = "select a.*, b.* from a.terms, b.impTerms";
connection.query(sqlQuery,
function(err,rows,fields){
if(err) throw err;
res.end(JSON.stringify(rows));
});

Change my database result to JSON

I would like to know how to get the result of my query to be a JSON response in Node.js. Currently, I am getting a result from my DB that is in JSON but I cannot access the values in it. My code is below.
connection.connect();
connection.query('select * from ttnews order by post_date DESC Limit 0,10',
function (error, results, fields) {
if (error) throw error;
console.log(results);
});
connection.end();
responseJSON.response = results[0].headline;
callback(null, responseJSON);
By the line responseJSON.response = results[0].headline I am getting an error results is undefined
Any help will be appreciated.
Try this code
var express = require('express');
var app = express();
var mysql = require('mysql');
var readline = require('readline');
var con = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'cmd'//your database name
});
con.connect(function(err){
if(err){
console.log('Error Connecting to Database');
}
});
app.get('/', function(req, res){
con.query('SELECT * FROM ttnews order by post_date DESC Limit 0,10',function(error, data){
if(error) {
console.log(error)
}else{
res.json(data)
}
});
});
app.listen(3000,function(){
console.log('server listening on 3000');
});
Hope this helps...
With Express 4.x, the output rows from mysql db is in json format.
For example,
sqlConnect.connection.query('SELECT * from users', function(err, rows, fields){
if (err) {
console.log("Querying error");
} else {
console.log(rows);
}
sqlConnect.connection.end();
});
The output is of the form
[ RowDataPacket {
id: 1,
username: 'Dani',
password: '1q2w3e4r',
verified: 0 } ]
So now you can get the individual values using the . operator. For example, console.log(rows[0].username) will log the value 'Dani'

Nested Query in Nodejs Mysql

I want to count a line in table that has FOO table.
The following code has a bug which show only the last db_name.
RESULT IS LOOK LIKE THIS:
db_0099,0
db_0099,5
db_0099,10
db_0099,3
Could you please suggest me how to fix the nodejs code?
var mysql = require('mysql');
var sql1 = "SELECT table_schema as db_name from information_schema.tables WHERE table_name = 'FOO' ";
var sql2 = "SELECT COUNT(*) as solution FROM {0}.FOO";
var connection = mysql.createConnection({
host : '$$$$$$$',
user : '$$$$$$$',
password : '$$$$$$$',
});
connection.connect(function(err){
console.log('connected as id ' + connection.threadId);
});
connection.query(sql1, function(err, result) {
if (err) throw err;
for (var i = 0, len = result.length; i < len; i++) {
var db_name = result[i].db_name;
console.log(db_name);
connection.query(sql2.replace("{0}",db_name), function(err, result) {
if (err) throw err;
console.log(db_name+','+result[0].solution); //Here db_name is showed only the last one.
});
};
connection.end();
});
i advice a two step solution to this problem:
use connection pooling
var pool = mysql.createPool({
host : 'xxxxx',
user : 'xxxxx',
password : 'xxxxx',
connectionLimit : 100
});
pool can do auto connection, so don't connect to your db, just
pool.query(sql,function(err,res){})
this way you use one connection for each query, which will be closed automatically after using it.
use async await for asyncronous sequential queries.
for that create a getResult function which returns a promise
function getResult(sql){
return new Promise(function(resolve,reject){
pool.query(sql, function(err, result){
if(err){
reject(err)
}else{
resolve(result)
}
})
})
}
then you can await each query in the loop
pool.query(sql1, async function(err, result) {
if (err) throw err;
for (var i = 0; i < result.length; i++) {
var db_name = result[i].db_name;
console.log(db_name);
var sql = sql2.replace("{0}",db_name)
var res = await getResult(sql)
console.log(db_name+','+res[0].solution); //Here db_name is showed only the last one.
};
pool.end()
});
P.S.: async await is a feature of the upcomming node 8.0.0 release in april. for node 7.x you will have to start your script with a commandline switch
node --harmony-async-await yourscript.js
Have you verify the content of result ?
console.log(result);
If it's okay try this :
solutions = results.map(result => {
let dbName = result.db_name;
let queryResult;
connection.query(sql2.replace("{0}", dbName), function(err, result) {
if (err) {
throw err;
} else {
queryResult = `${db_name}, ${result[0].solution}`
console.log(queryResult);
}
});
return queryResult;
})
console.log(solutions);
However, try to use a ORM or a sql parser for your query !
Try this one :)
https://hiddentao.com/squel/

Nodejs - MySQL : How to get a specific result back to nodejs

Say I have a query like below...
SELECT
Username AS name,
Occupation AS occ,
Hobby AS hob
FROM
mt_User
At my Nodejs segment, I have...
conn.getConnection(
function (err, client) {
if(err){
console.log('Connection Error');
throw err;
}
client.query(thatQueryAbove, function(err, rows){
if(err){
console.log('Query Error');
}
var Username = rows???????
var Occupation = rows??????
var Hobby = rows???????
....How exactly do I call back the data with the alias
defined in my query like 'name', 'occ' and 'hob'?
});
});
Can anyone lemme how to retrieve the data back using the alias of my SELECT statement in Node.js?
Thank you in advance guys :)
You can use the standard Node.js driver for mysql. The code to accomplish above will look like below
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'mysqluser',
password : 'yourpass',
database : 'dbname'
});
connection.connect();
connection.query('SELECT Username AS name,Occupation AS occ,Hobby AS hob FROM mt_User', function(err, rows, fields) {
if (err) throw err;
if(rows && rows.length>0)
console.log(rows[0].name, rows[0].occ, rows[0].hob);
else
console.log('No Results Found!');
});

ExpressJs render after an action

i'm testing ExpressJs and i have a problem.
var mysql = require('mysql');
var url = require('url');
var connection = mysql.createConnection({
host : 'localhost',
port : '8889',
user : 'root',
password : 'root',
database : 'test'
});
var results = '';
// INIT
exports.init = function(req, res) {
if (req.params.query == 'names') {
getByName(req, res);
} else {
res.send('Erreur');
}
}
getByName = function(req, res) {
currentUrl = url.parse(req.url);
getResult = req.params.suffix.split('+');
for (key in getResult) {
connection.query('SELECT * from testnode WHERE nom = "'+getResult[key]+'"', function(err, rows, fields) {
if (err) throw err;
results += JSON.stringify(rows[0]);
console.log(results);
});
}
res.render('api', {'results' : results});
}
When i go for the first time on the page this one is empty and if i refresh the result appear.
I don't know why the first time the variable "results" are empty so the console.log give me the good result.
Have you got any ideas ?
Thanks a lot :)
You error comes from the mix of a loop and a callback. Node.js is a non blocking IO library : the process doesn't wait for your mysql query to finish to continue to do other stuffs, so the callback with the results is executed (sometime) after the render.
You have multiple options, the one I use is https://github.com/caolan/async or call render once all the callback are done.
Or change your strategy:
getByName = function(req, res) {
currentUrl = url.parse(req.url);
getResult = req.params.suffix.split('+');
connection.query('SELECT * from testnode WHERE nom IN ("' + getResult.join('",") + '")', function(err, rows, fields) {
if (err) throw err;
var results = JSON.stringify(rows); //get all the results
res.render('api', {'results' : results});
});
}