How do I INSERT into a mysql database using data obtained through a SELECT query?
Using node.js and the mysql module, I am trying to select data from a table, manipulate that data, then insert the manipulate data to another table in the database.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
for(var i = 0; i < 100; i++) { //let's say there are 100 rows in table1
var manipulated_data = result + 1;
data_wanted.push(manipulated_data);
}
});
Ideally, I would take the values in data_wanted and bulk insert it. Something like:
var q2 = 'INSERT into table2 (number) VALUES ?';
connection.query(q2, [data_wanted], function(err, result) {
//bulk inserts data_wanted
});
However, data_wanted does not take data from the first query.
console.log(data_wanted) //outputs ; still empty, even after 1st query
I believe that this is a scope issue. But I have no idea how to actually solve this problem. I would appreciate help greatly.
javascript mysql node.js
add a comment |
Using node.js and the mysql module, I am trying to select data from a table, manipulate that data, then insert the manipulate data to another table in the database.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
for(var i = 0; i < 100; i++) { //let's say there are 100 rows in table1
var manipulated_data = result + 1;
data_wanted.push(manipulated_data);
}
});
Ideally, I would take the values in data_wanted and bulk insert it. Something like:
var q2 = 'INSERT into table2 (number) VALUES ?';
connection.query(q2, [data_wanted], function(err, result) {
//bulk inserts data_wanted
});
However, data_wanted does not take data from the first query.
console.log(data_wanted) //outputs ; still empty, even after 1st query
I believe that this is a scope issue. But I have no idea how to actually solve this problem. I would appreciate help greatly.
javascript mysql node.js
add a comment |
Using node.js and the mysql module, I am trying to select data from a table, manipulate that data, then insert the manipulate data to another table in the database.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
for(var i = 0; i < 100; i++) { //let's say there are 100 rows in table1
var manipulated_data = result + 1;
data_wanted.push(manipulated_data);
}
});
Ideally, I would take the values in data_wanted and bulk insert it. Something like:
var q2 = 'INSERT into table2 (number) VALUES ?';
connection.query(q2, [data_wanted], function(err, result) {
//bulk inserts data_wanted
});
However, data_wanted does not take data from the first query.
console.log(data_wanted) //outputs ; still empty, even after 1st query
I believe that this is a scope issue. But I have no idea how to actually solve this problem. I would appreciate help greatly.
javascript mysql node.js
Using node.js and the mysql module, I am trying to select data from a table, manipulate that data, then insert the manipulate data to another table in the database.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
for(var i = 0; i < 100; i++) { //let's say there are 100 rows in table1
var manipulated_data = result + 1;
data_wanted.push(manipulated_data);
}
});
Ideally, I would take the values in data_wanted and bulk insert it. Something like:
var q2 = 'INSERT into table2 (number) VALUES ?';
connection.query(q2, [data_wanted], function(err, result) {
//bulk inserts data_wanted
});
However, data_wanted does not take data from the first query.
console.log(data_wanted) //outputs ; still empty, even after 1st query
I believe that this is a scope issue. But I have no idea how to actually solve this problem. I would appreciate help greatly.
javascript mysql node.js
javascript mysql node.js
asked Nov 15 '18 at 7:29
ssorssor
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It should be this
var q2 = 'INSERT into table2 (number) VALUES (?)';
I think its just the brackets you were missing.
You are using statements so you will have to add more code of course.
I hope I helped.
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
add a comment |
You can use one query insert all.
This a example:
INSERT INTO users_log (`name`,`address`)
SELECT u.`name`,u.`address`
FROM users u ;
Table users:
Table users_log:
This link documment: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
add a comment |
The main cause because your code doesn't have connect to database .You must to connect database before when you do query sql. You can refer this way.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
result.forEach(function(row) {
console.log(row);
data_wanted.push(row);
});
console.log(data_wanted);
});
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314375%2fhow-do-i-insert-into-a-mysql-database-using-data-obtained-through-a-select-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It should be this
var q2 = 'INSERT into table2 (number) VALUES (?)';
I think its just the brackets you were missing.
You are using statements so you will have to add more code of course.
I hope I helped.
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
add a comment |
It should be this
var q2 = 'INSERT into table2 (number) VALUES (?)';
I think its just the brackets you were missing.
You are using statements so you will have to add more code of course.
I hope I helped.
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
add a comment |
It should be this
var q2 = 'INSERT into table2 (number) VALUES (?)';
I think its just the brackets you were missing.
You are using statements so you will have to add more code of course.
I hope I helped.
It should be this
var q2 = 'INSERT into table2 (number) VALUES (?)';
I think its just the brackets you were missing.
You are using statements so you will have to add more code of course.
I hope I helped.
answered Nov 15 '18 at 7:33
kimo26kimo26
806
806
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
add a comment |
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
My problem is that is that the data I tried extract from the first query (a SELECT query) is, in fact, not extracted. I have no data to insert in the first place, so I don't believe the INSERT query is the problem.
– ssor
Nov 15 '18 at 8:03
add a comment |
You can use one query insert all.
This a example:
INSERT INTO users_log (`name`,`address`)
SELECT u.`name`,u.`address`
FROM users u ;
Table users:
Table users_log:
This link documment: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
add a comment |
You can use one query insert all.
This a example:
INSERT INTO users_log (`name`,`address`)
SELECT u.`name`,u.`address`
FROM users u ;
Table users:
Table users_log:
This link documment: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
add a comment |
You can use one query insert all.
This a example:
INSERT INTO users_log (`name`,`address`)
SELECT u.`name`,u.`address`
FROM users u ;
Table users:
Table users_log:
This link documment: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
You can use one query insert all.
This a example:
INSERT INTO users_log (`name`,`address`)
SELECT u.`name`,u.`address`
FROM users u ;
Table users:
Table users_log:
This link documment: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
edited Nov 15 '18 at 7:57
answered Nov 15 '18 at 7:43
son phamson pham
27816
27816
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
add a comment |
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
I can't use INSERT SELECTs because, in reality, I want to manipulate selected data with complex conditionals. The result+1 is really just an example.
– ssor
Nov 15 '18 at 8:00
add a comment |
The main cause because your code doesn't have connect to database .You must to connect database before when you do query sql. You can refer this way.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
result.forEach(function(row) {
console.log(row);
data_wanted.push(row);
});
console.log(data_wanted);
});
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
|
show 2 more comments
The main cause because your code doesn't have connect to database .You must to connect database before when you do query sql. You can refer this way.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
result.forEach(function(row) {
console.log(row);
data_wanted.push(row);
});
console.log(data_wanted);
});
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
|
show 2 more comments
The main cause because your code doesn't have connect to database .You must to connect database before when you do query sql. You can refer this way.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
result.forEach(function(row) {
console.log(row);
data_wanted.push(row);
});
console.log(data_wanted);
});
The main cause because your code doesn't have connect to database .You must to connect database before when you do query sql. You can refer this way.
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'pass',
database: 'db',
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var q = 'SELECT * FROM table1';
var data_wanted = ;
connection.query(q, function(err, result) {
result.forEach(function(row) {
console.log(row);
data_wanted.push(row);
});
console.log(data_wanted);
});
edited Nov 16 '18 at 2:12
answered Nov 15 '18 at 8:12
thanhdung0312thanhdung0312
31627
31627
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
|
show 2 more comments
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
I can most definitely access the data within connection.query() so it's definitely connected to my db. But I tried adding what you said anyway, and sadly it didn't work :( My problem is definitely with scope. Adding onto data_wanted inside the anonymous function within the .query parameters does not work. The array gets thrown out. No idea how to do it otherwise, though.
– ssor
Nov 15 '18 at 8:53
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor I changed code to forEach. you can refer
– thanhdung0312
Nov 15 '18 at 9:17
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
@ssor please info to me if it works
– thanhdung0312
Nov 15 '18 at 9:20
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
still doesn't work. data_wanted is still empty.
– ssor
Nov 15 '18 at 19:31
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
@ssor I edited your code. the cause is async nodejs. you can put console.log(data_wanted); inside connection.query method . it will show result
– thanhdung0312
Nov 16 '18 at 1:27
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314375%2fhow-do-i-insert-into-a-mysql-database-using-data-obtained-through-a-select-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown