nodejs
  1. nodejs-mysql-update

Node.js MySQL Update

When working with databases, it is often necessary to update existing data. In Node.js, we can use the MySQL module to update records in a MySQL database. In this tutorial, we'll discuss how to update data in a MySQL database using Node.js.

Syntax

The basic syntax for updating data in a MySQL database using Node.js is as follows:

connection.query('UPDATE table_name SET column1 = ?, column2 = ?, ... WHERE condition', [value1, value2, ...], function (error, results, fields) {
    // callback function
});

In this syntax:

  • table_name is the name of the table that you want to update.
  • column1, column2, etc. are the names of the columns that you want to update.
  • value1, value2, etc. are the new values that you want to set for the columns.
  • condition is the condition that must be met in order for the update to take place.
  • error, results, and fields are callback functions that handle any errors and results that are returned from the query.

Example

Let's say we have a users table in our MySQL database with columns id, name, and email. We want to update the email of a user with an id of 1 to be newemail@example.com. Here's how we can implement it using Node.js:

const mysql = require('mysql');

const connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'password',
    database : 'mydb'
});

connection.connect();

connection.query('UPDATE users SET email = ? WHERE id = ?', ['newemail@example.com', 1], function (error, results, fields) {
    if (error) throw error;
    console.log('Updated ' + results.changedRows + ' rows');
});

connection.end();

In this example, we first establish a connection to our MySQL database using the mysql module. We then send an SQL query to update the email value of the user with an id of 1. We set the value of the email column to newemail@example.com and specify the condition that the id must be equal to 1. We handle any errors that may occur in the error callback function and print the number of rows that were updated in the results callback function. Finally, we close the connection to the database.

Output

When we run the example code above, the output will be:

Updated 1 rows

This is because we updated the email of the user with an id of 1, and only one row was affected.

Explanation

In the example above, we used the mysql module to connect to our MySQL database and update the email value of a user with an id of 1. We specified the table that we wanted to update, set the new value for the email column, and specified the condition that the id must be equal to 1. We used a callback function to handle any errors and print the number of rows that were updated. Finally, we closed the connection to the database.

Use

Updating records in a MySQL database is a common operation when working with databases. You can use the Node.js mysql module to easily update records in your database, and handle any errors that may occur.

Important Points

  • When using connection.query(), always use placeholders to prevent SQL injection attacks.
  • Always handle errors and results in the callback functions when using connection.query().
  • Close the connection to the database when you're done with it.

Summary

In this tutorial, we discussed how to update data in a MySQL database using Node.js. We covered the syntax, example, output, explanation, use, and important points of updating data in a MySQL database using Node.js. With this knowledge, you can now update records in your own MySQL databases using Node.js.

Published on: