-
-
Save dineshsonachalam/677fb4b256b3bf158efaad98c4aa73d9 to your computer and use it in GitHub Desktop.
Using Node to export MySQL query results to a file as JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/felixge/node-mysql | |
// npm install mysql | |
var mysql = require('mysql'); | |
// http://nodejs.org/docs/v0.6.5/api/fs.html#fs.writeFile | |
var fs = require('fs'); | |
var client = mysql.createClient({ | |
user: 'root', | |
password: 'mysqlpassword' | |
}); | |
client.query('select * from db.table;', function(err, results, fields) { | |
if(err) throw err; | |
fs.writeFile('table.json', JSON.stringify(results), function (err) { | |
if (err) throw err; | |
console.log('Saved!'); | |
}); | |
client.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment