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();
});
Related
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
Trying to load data from web services using web data connector into a table in NodeJS and then loading this table into DB. But there is code error stating
Unchanged ReferenceError: require is not defined.
Tried to change the directory for require function dint help. But it works without using the web data connector provided by tableau.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "list"
});
//fs.readFile('list.json', 'utf8', function (err, data)
{
if (err) throw err;
var obj = JSON.parse(tableData);
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
for(var i = 0; i < obj.length; i++) {
var s_at = obj[i].id;
var sql = 'INSERT INTO members (scraped_at) VALUES ('+s_at+');
con.query(sql, function (err, result) {
if (err) throw err;
console.log("x record inserted");
});
}
I need this code to load the id into a table in my database
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 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);
});
});
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);
});