Created
December 12, 2020 17:16
-
-
Save ricardoaguiar/1db388162936a2f8458ee27028b5fdb3 to your computer and use it in GitHub Desktop.
Talking to a remote mysql database using node.js
This file contains 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
// | |
// Hacked together from http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/ | |
// | |
var Client = require('mysql').Client; | |
var client = new Client(); | |
client.host ='some.host.com'; | |
client.user = 'user'; | |
client.password = 'password'; | |
console.log("connecting..."); | |
client.connect(function(err, results) { | |
if (err) { | |
console.log("ERROR: " + err.message); | |
throw err; | |
} | |
console.log("connected."); | |
clientConnected(client); | |
}); | |
clientConnected = function(client) | |
{ | |
tableHasData(client); | |
} | |
tableHasData = function(client) | |
{ | |
client.query( | |
'SELECT * FROM [db].[table] LIMIT 0,10', | |
// you can keep this function anonymous | |
function (err, results, fields) { | |
if (err) { | |
console.log("ERROR: " + err.message); | |
throw err; | |
} | |
console.log("Got "+results.length+" Rows:"); | |
for(var i in results){ | |
console.log(results[i].[column name]); | |
console.log('\n'); | |
//console.log("The meta data about the columns:"); | |
//console.log(fields); | |
} | |
client.end(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment