i have a question regarding about node.js for mysql.
I'm trying to check for changes in the database with node.js and every time it does a change it would do something.
I have the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'socialpodd',
});
connection.connect();
connection.query('SELECT * FROM personal', function(err, rows, fields) {
if (err) throw err;
console.log('Rows: ', rows[0].email);
});
connection.end();
But I want this code to be called once there is a change in the database. I know I could do a poll method, but it doesn't seem that effective and real-time. What would be a good real-time module that would allow me to do it, or is polling my only option?
Related
I am using mysql and request. I want to make a request in my localhost server. I just need to know how to make the get request into the localhost server because I don't understand how, I'm running into a continuous loop.
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'virtual_currency_price'
});
Request:
router.get('/getprice', function(req, res){
request(connection, function (error, response, body) {
if (!error && response.statusCode == 200){
}
});
});
I'm trying to figure out how to do this but I'm failing very miserably
The table I'm trying to GET from is called "price" and I will be using the datetime to fetch certain prices.
After you establish a connection with your server you can use it to fetch data from database. You don't need to make a request using the module, just use the connection directly to execute queries as below:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'virtual_currency_price'
});
connection.connect();
connection.query('SELECT * from price', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
});
connection.end();
It's recommended that you create a pool of connections instead and reuse them in your app instead of creating it every time you need to access the DB.
See the docs from github.
I'm using auth0 as my authentification services into my project. I really love it, but I have a problem when using custom database(MySql), I sure that I have configured the db.connection parameter to my remote shared hosting database in Plesk. It always show : "[Error] Script execution did not complete within 20 seconds. Are you calling the callback function?", When I trying to run "Create" script.
here the script :
function create (user, callback) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.23.16',
user : 'user',
password : 'pass',
port : '3306',
database : 'dbname' });
connection.connect();
var query = "INSERT INTO users SET ?";
var insert = {
password: bcrypt.hashSync(user.password, 10),
email: user.email
};
connection.query(query, insert, function (err, results) {
if (err) return callback(err);
if (results.length === 0) return callback();
callback(null);
});
}
What should I'm doing right now to solve this problem? I'm new to this
Thanks..
Regards,
fxbayuanggara
You're trying to connect to a local IP address (192.168.23.16), which will always fail since database scripts and rules are executed from Auth0's servers. You'll need to make your MySQL server accessible from Auth0's IP addresses, which at the time of writing are the following:
US domains: 138.91.154.99, 54.221.228.15, 54.183.64.135, 54.67.77.38, 54.67.15.170, 54.183.204.205, 54.173.21.107, 54.85.173.28
EU domains: 52.28.56.226, 52.28.45.240, 52.16.224.164, 52.16.193.66
Can someone please show me an example of how we use phonegap and node.js to push and pull data to mySQL database from an html page/form .
THANKS
EDIT
var connection = mysql.createConnection({ host : 'localhost', user : 'root', database :'nodejsmysql', password : '' });
connection.connect();
connection.query('SELECT * from nodejs', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].fname); }
);
connection.end();
I have the above code which works very well, But I dont want to display my results on the console, instead I want to display them on an html textBox
You need to use custom web service to communicate with your server. You cannot directly manipulate the data on client side. The data manipulation happen only at server side. So, either you will use REST or SOAP, it's up to you how you want to implement it. I suggest you doing more research about the web service.
REST - http://en.wikipedia.org/wiki/Representational_state_transfer
SOAP - http://en.wikipedia.org/wiki/SOAP
More
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
http://blog.revivalx.com/2014/02/25/crud-operation-using-jquery-mobile-on-android-part-2/
var express = require('express');
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodejsmysql'
});
app.use(express.bodyParser());
app.post('/index', function(req, res) {
var date = req.body.date;
connection.query('INSERT INTO reservations(Date) VALUES (? );' , [date], function(err, docs) {
if (err) res.json(err);
res.redirect('users');
});
});
I managed to nail it using this!
I am using node-mysql for the first time, and I have a program with no errors, no warnings, but is not working properly... Here is the code for the program, very simple:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database: 'nodetest',
port: 8080
});
connection.connect();
var usr = "BLASHDASD"
var userId = usr;
var sql = 'UPDATE userlist SET user1= ' + connection.escape(userId) + ' WHERE id=1 ';
console.log(sql);
connection.query('sql', function(err, rows, fields) {
console.log(err);
console.log("BLAHSDBKASD");
});
connection.end();
And here is the console output:
C:\wamp\www\nodePHP-master\js>node nodeTest.js
UPDATE userlist SET user1= 'BLASHDASD' WHERE id=1
But nothing is happening in my MySQL table... I even copied and pasted the UPDATE line above and just ran it as SQL code and it worked great... Need some ideas of what is going on. Thanks a bunch
EDIT:
Answered my own question... was listening on wrong port, so connection was failing. Here is updated code for those interested/search in the future:
//TEST
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database: 'nodetest',
port: 3306,
});
connection.connect(function(err){
if(err) {
console.log(err)
} else {
console.log("connected");
}
});
var usr = "BLASHDASD"
var userId = usr;
var sql = 'UPDATE userlist SET user1= ' + connection.escape(userId) + ' WHERE id=1 ';
console.log(sql);
connection.query(sql, function(err, rows, fields) {
console.log(err);
});
connection.end();
You are having problems with node's asynchronous nature, a very common issue when coming to Node. You also had a small but significant error in your code (you have 'sql' as a quoted string), but here is something structurally similar that should point you in the right direction.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'locahost',
user : 'foo',
password : 'bar',
database : 'test'
});
// the callback inside connect is called when the connection is good
connection.connect(function(err){
var sql = "select 'Joe' as name from dual";
connection.query(sql, function(err, rows, fields) {
if (err) return console.log(err);
// you need to end your connection inside here.
connection.end();
console.log(rows[0].name);
});
});
You will likely start wondering about ways to avoid all these callbacks. You may wish to look at my answer to this question for a more extended mysql example as well as an alternative implementation which offers an alternative to callback-mania.
Is there a way to import data from MySQL database on Meteor startup? I basically need just an initial data from MySQL to export it to Mongo collections for usage.
Your best bet might be to just use a mysql node package (remember to use Meteor.npmRequire(..) instead of require(..)). This one seems good:
https://github.com/felixge/node-mysql
Something like this should work:
if (Meteor.isServer) {
var mysql = Meteor.npmRequire('mysql');
Meteor.startup(function() {
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
connection.query('SELECT * FROM table', function(err, rows, fields) {
if (err) throw err;
// create documents from rows[i] and add to your collection
});
connection.end();
});
}