Languages
[Edit]
EN

Node.js - MySQL Update query

0 points
Created by:
illona
526

In this article, we would like to show you how to make an SQL UPDATE query in Node.js.

Note: at the end of this article you can find database preparation SQL queries, you will also find an example of a more parameterized query there.

1. UPDATE query example

const mysql = require('mysql');

const connection = mysql.createConnection({               // gets connection with database
    host: 'localhost',  // '127.0.0.1'
    user: 'root',
    password: 'password',
    database: 'my_database',
});

connection.connect(error => {
    if (error) throw error;
    const query = 'UPDATE `users` '+
                  'SET `name` = ?, `role` = ? ' +
                  'WHERE `id` = ?';
    const values = ['Christopher', 'admin', '2'];

    connection.query(query, values, (error, result) => {  // sends queries
        connection.end();                                 // closes connection
        if (error) throw error;
        console.log(connection.sql);  // UPDATE `users` 
    });                              // SET `name` = 'Christopher, `role` = 'admin' 
});                                  // WHERE `id` = '2'

Before:

[
    { id: 1, name: 'John',  role: 'admin' },     
    { id: 2, name: 'Chris', role: 'moderator' },
    { id: 3, name: 'Kate',  role: 'user' },      
    { id: 4, name: 'Denis', role: 'moderator' } 
]

After:

[
    { id: 1, name: 'John',  role: 'admin' },     
    { id: 2, name: 'Christopher', role: 'admin' },
    { id: 3, name: 'Kate',  role: 'user' },
    { id: 4, name: 'Denis', role: 'moderator' }
]

2. Database preparation

create_tables.sql file:

CREATE TABLE `users` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(100) NOT NULL,
	`role` VARCHAR(15) NOT NULL,
	PRIMARY KEY (`id`)
)
ENGINE=InnoDB;

insert_data.sql file:

INSERT INTO `users`
	(`name`, `role`)
VALUES
	('John', 'admin'),
	('Chris', 'moderator'),
	('Kate', 'user'),
	('Denis', 'moderator');

 More parameterized query

// ...
connection.connect(error => {
    if (error) throw error;
    const query = `UPDATE ??
                   SET ?? = ?, ?? = ? 
                   WHERE ?? = ?`;
    const values = ['users', 'name', 'Christopher', 'role', 'admin', 'id', '2'];

    connection.query(query, values, (error, result) => {  // sends queries
        connection.end();                                 // closes connection
        if (error) throw error;
        console.log(connection.sql);  // UPDATE `users` 
    });                               // SET `name` = 'Christopher, `role` = 'admin' 
});                                   // WHERE `id` = '2'

See also:

  1. Node.js - MySQL Insert query
  2. Node.js - MySQL Delete query

  3. Node.js - MySQL Select query

Alternative titles

  1. Node.js - how to make MySQL Update query?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - MySQL

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join