Wednesday, 8 February 2017

Redis Commands with examples

Question: How to set the data with hset?
hset testdata name "Arun Kumar"



Question: How to get the data with hset?
hget testdata name



Question: How to set more data with hset?
hset testdata age "12"
hget testdata age



Question: How to get all data which was set with hset?
hgetall testdata

Output
1) "name"
2) "Arun Kumar"
3) "age"
4) "30"



Question: How to delete data which was set with hset?
hdel testdata age



Question: How to get all data which was set with hset?
hgetall testdata
Command
1) "name"
2) "Arun Kumar"


Redis Commands with examples

Question: How to insert data with lpush ?
lpush usernames "Arun"
lpush usernames "Tarun"
lpush usernames "Karan"



Question: How to list data which was set with lpush ?
lrange usernames 0 10
1)"Arun"
2)"Tarun"
3)"Karan"



Question: How to insert data with zadd?
zadd testdata1 0 "Zero" 
zadd testdata1 1 "One" 
zadd testdata1 2 "Two" 
1) "zero"
2) "ONe"
3) "Two"



Question: Difference between io.sockets.emit and socket.broadcast.emit?
io.sockets.emit will send to all the clients.
socket.broadcast.emit will send the message to all the other clients except the newly created connection.

Friday, 3 February 2017

How to execute MySQL queries in node.js?

How to execute MySQL queries in node.js?

Question: How to install mysql in nodeJS?
npm install mysql



Question: How to update mysql in nodeJS?
npm update mysql



Question: How to include MySQL in nodeJS?
var mysql = require("mysql");



Question: How to connect to MySQL in nodeJS?
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "mydb"
});



Question: How to handle error while connecting to MySQL in nodeJS?
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "mydb"
});
con.connect(function(err) {
    if (err) {
        console.log('Error connecting to database');
        return;
    }
    console.log('Connection established Successfully');
});



Question: How to insert record in database with nodeJS?
var post = {
    'userID': "1",
    'userName': 'username',    
    'device': 'web',
    'stream_time': "0",
    'user_display_name': "arun kumar gupta",
    'user_photo': "hello"
};
var mysqlProfiler = con.query('INSERT INTO messages set ?', post, function(err, result) {});
console.log(mysqlProfiler.sql);



Question: How to print SQL query in nodejs?
var mysqlProfiler = con.query('INSERT INTO messages set ?', post, function(err, result) {});
console.log(mysqlProfiler.sql);



Question: How to insert record in database - Full Example?
var mysql = require("mysql");
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "mydb"
});
var post = {
    'userID': "1",
    'userName': 'username',    
    'device': 'web',
    'stream_time': "0",
    'user_display_name': "arun kumar gupta",
    'user_photo': "hello"
};
var mysqlProfiler = con.query('INSERT INTO messages set ?', post, function(err, result) {});
console.log(mysqlProfiler.sql); //print the query



Question: How to fetch all records in nodeJS?
var uid=1002;
 mysqlConn.query('SELECT * FROM `messages` WHERE id = '+uid , function (error, results, fields) {
    if (results != null && results.length > 0) {
        for each (var results in res) {
            console.log(res);
         }
    }
}