Tutorial by Examples

When you want to use user generated content in the SQL, it with done with parameters. For example for searching user with the name aminadav you should do: var username = 'aminadav'; var querystring = 'SELECT name, email from users where name = ?'; connection.query(querystring, [username], functi...
a. Running multiple queries at same time All queries in MySQL connection are done one after another. It means that if you want to do 10 queries and each query takes 2 seconds then it will take 20 seconds to complete whole execution. The solution is to create 10 connection and run each query in a di...
One of the easiest ways to connect to MySQL is by using mysql module. This module handles the connection between Node.js app and MySQL server. You can install it like any other module: npm install --save mysql Now you have to create a mysql connection, which you can later query. const mysql ...
You send the query as a string and in response callback with the answer is received. The callback gives you error, array of rows and fields. Each row contains all the column of the returned table. Here is a snippet for the following explanation. connection.query('SELECT name,email from users', fu...
There may be situations where you have setup a pool of MySQL connections, but you have a number of queries you would like to run in sequence: SELECT 1; SELECT 2; You could just run then using pool.query as seen elsewhere, however if you only have one free connection in the pool you must wait un...
You can attach the query executed to your err object when an error occurs: var q = mysql.query('SELECT `name` FROM `pokedex` WHERE `id` = ?', [ 25 ], function (err, result) { if (err) { // Table 'test.pokedex' doesn't exist err.query = q.sql; // SELECT `name` FROM `pokedex` WHERE `id` ...
// db.js const mysql = require('mysql'); const pool = mysql.createPool({ connectionLimit : 10, host : 'example.org', user : 'bob', password : 'secret', database : 'my_db' }); module.export = { getConnection: (callback) => { retu...

Page 1 of 1