Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Mysql Update in NodeJs

Mysql Update in NodeJs

To update records in MySQL using Node.js, you can use the UPDATE SQL statement with either the mysql or mysql2 package.

Here’s a complete example to get you going 👇


✅ Step 1: Install MySQL package

bash

npm install mysql const mysql = require('mysql');const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase'});connection.connect();const sql = 'UPDATE users SET age = ? WHERE name = ?';const values = [32, 'Bob'];connection.query(sql, values, (err, result) => { if (err) throw err; console.log('Rows updated:', result.affectedRows); connection.end();});


Using mysql2 (Async/Await)

js

const mysql = require('mysql2/promise');async function updateUser() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase' }); const sql = 'UPDATE users SET age = ? WHERE name = ?'; const values = [28, 'Alice']; const [result] = await connection.execute(sql, values); console.log('Rows updated:', result.affectedRows); await connection.end();}updateUser();


🧠 Tips

  • Always use ? placeholders to prevent SQL injection.

  • Check result.affectedRows to confirm how many rows were updated.

  • You can update multiple fields like:

sql

UPDATE users SET name = ?, age = ? WHERE id = ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql