I have question. How to connect dbconnection.js and demo_api_select.js I have a problem when declare variable on demo_api_select.js. It doesn't work and have error notification like this:
Error Notification
Please help
dbconnection.js:
var mysql=require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
module.exports = con;
demo_api_select.js
var db = require('./dbconnection');
db.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
I want to make those code work as well as this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err) {
if (err) throw err;
//Select all customers and return the result object:
con.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Try this code.No need to connect twice to database
var db = require('./dbconnection');
db.query("SELECT * FROM profil" , function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Related
How to insert sql query data in array using node js
i want to insert data in mysql database and also in array using node js
please give proper solution.
var conn = mysql.createConnection({
host: "localhost",
user: "root",
password: "root#123",
database: "abc"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var temp = [1, 2];
sql =
"INSERT INTO student(`name`, `email`) VALUES (?) ";
conn.query(sql, [temp], function (err, result) {
console.log(temp);
if (err) throw err;
conn.end();
});```
Hey please change your code like this...
var conn = mysql.createConnection({
host: "localhost",
user: "root",
password: "root#123",
database: "abc"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var temp = [1, 2];
var sql =
"INSERT INTO student(`name`, `email`) VALUES (?,?) ";
conn.query(sql, temp, function (err, result) {
console.log(temp);
if (err) throw err;
conn.end();
});
I am creating a facebook chatbot using node js and to store data I used MySQL Database. Currently it is working fine. But, I have a question that, should I need to close the database connection?
I tried to do it but when I close the connection then in the next attempt it is throwing an error that No SQL Connection found
Please tell me the correct way of how to close database connection and reuse it
let databaseConnection = () =>{
let conn = mysql.createConnection({
host: "",
user: "",
password: "",
database: "",
port: ""
});
conn.connect(function (error) {
if (error) {
console.log("Error when connecting to db ",error);
setTimeout(databaseConnection, 2000);
}
else {
console.log("Connected");
}
});
conn.on('error',function(err){
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
databaseConnection();
}else{
throw err;
}
});
return conn;
}
conn = databaseConnection();
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
conn.end()
}
You should end your connection after the loop
for (let i=0;i<10;i++){
query = "SELECT name FROM info where id=?";
value = i
conn.query(query,value, function (err, result, fields) {
if (err) throw err;
console.log(result);
console.log("Data Fetched Successfully.")
})
}
conn.end()
i got the above error
my code was like this
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
con.end();
i tried to do as they explain in the below link but i did not know how to find my socket path?
enter link description here
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
in my c# I have
IRestResponse response = client.Execute(request);
var content = response.Content;
but the content prints "Internal Error" for all err types
EDIT
Also tried:
pool.getConnection((err, conn) => {
if(err) {
var error = new Error("something is wrong");
callback(error);
}
according to this https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html, still getting Internal Error for all error types
Also, I do get 200 OK on good requests, what I am trying to do is to print an explanatory message to the client in case of an error
I was wondering how to execute a mysql query so that the client can see like "users registered: number"
Server.js:
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'kitsune',
connectionLimit: 50,
port: 3306 });
connection.connect(function(err) {
if (err) throw err;
});
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
module.exports = connection;
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var countQuery = "SELECT Count(ID) AS NumberOfPenguins FROM Penguins"; console.log(countQuery);
connection.query(countQuery, function(err, rows) {
if (err) throw err;
console.log('Connected!');
});
var renderedHtml = ejs.render(content, {countQuery: countQuery});
res.end(renderedHtml);
});
}).listen(80);
index.html:
<html>
<head>
</head>
<body>
Users registered: <%= countQuery %>
</body>
</html>
This only shows the query but it does not execute it. Any idea on how to execute it?
You executed the query sure but you never did anything with the results. The result is the rows passed in the callback function.
connection.query(countQuery, function(err, rows) {
if (err) throw err;
var renderedHtml = ejs.render(content, {countQuery: rows});
res.end(renderedHtml);
});