1

I have a very simple question that I haven't been able to solve for days. I wrote the following simple code block to give an example.

var mysql = require("mysql")

var connection = mysql.createConnection({
  host: "localhost",
  user: "***",
  password: "***",
  database: "***",
  charset: "utf8mb4"
})

var testArr = ["banana", "apple", "orange", "strawberry"]

for (let fruit of testArr) {
    console.log("fruit check:" + fruit)

    var fruitName = connection.escape(fruit)

    var sql = "INSERT INTO test (name) VALUES (" + fruitName + ")";

    connection.query(sql, function (err, result) {
        if (err) throw err;
        console.log("1 record inserted");
    });
}

The only thing I want to do here is to actually proceed sequentially after entering the for loop. At the moment, the console side comes as follows;

fruit check:banana
fruit check:apple
fruit check:orange
fruit check:strawberry
1 record inserted
1 record inserted
1 record inserted
1 record inserted

I want it to come like this:

fruit check:banana
1 record inserted
fruit check:apple
1 record inserted
fruit check:orange
1 record inserted
fruit check:strawberry
1 record inserted
2
  • You can try to use mysql2 module instead of mysql. It supports promises so you can use async/await. In the other hand you could build a query in the for loop so it looks like this INSERT INTO test (name) VALUES ("banana"), ("apple"), (...) and execute it outside the for loop as a single query. Commented May 30, 2022 at 5:52
  • Hey Molda, thanks for your return. I tried a bit but I couldn't convert the above code to what I wanted. can you apply the solution to this code block? Thus, we can say that this is the result we will get when we write it this way in its most basic form. Commented May 30, 2022 at 6:35

2 Answers 2

1

Try to use promise when you connection your mysql,and use async/await.

you can get approximate answer here

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for your return. Can you apply the solution to the simple code block above? Thus, we can say that this is the result we will get when we write it this way in its most basic form.
0

Try this approach with async - await and mysql2.
I changed the query to a prepared statement.

const mysql = require('mysql2/promise');

const loadData = async () => {
  try {
    var connection = await mysql.createConnection({
      host: 'localhost',
      user: '***',
      password: '***',
      database: '***',
      charset: 'utf8mb4',
    });

    var testArr = ['banana', 'apple', 'orange', 'strawberry'];

    for (let fruit of testArr) {
      console.log('fruit check:' + fruit);
      var sql = 'INSERT INTO test (name) VALUES (?)';
      await connection.execute(sql, [fruit])
      console.log('1 record inserted');
    }
  } catch (err) {
    throw err; // Or do something else with the error...
  }
};

loadData();

1 Comment

the exact answer to the question, thank you @lpizzinidev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.