Created
January 3, 2018 16:28
-
-
Save jerosoler/85cff8c7006faeda636b3e7c14354bbb to your computer and use it in GitHub Desktop.
Node mysql auto connect and close
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
var mysql = require('mysql'); | |
var db_config = { | |
host : 'localhost', | |
user : 'usuario', | |
password : 'password', | |
database : 'ddbb' | |
}; | |
var connection = {}; | |
connection.query = function (sql, callback) { | |
var _err; | |
var _rows; | |
var conn = mysql.createConnection(db_config); | |
conn.query(sql, function (err, rows) { | |
_err = err; | |
_rows = rows; | |
}); | |
conn.end(function () { | |
callback(_err, _rows); | |
}); | |
}; | |
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) { | |
if (error) throw error; | |
console.log('The solution is: ', results[0].solution); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment