Database IntegrationLesson 56Free preview
MySQL Update
This lesson is part of Node.js · All levels
Summary
This lesson explains how to update data in the MySQL database from the Node.js application using the mysql2 module. You'll use the SQL UPDATE command with the binding parameters to be safe from SQL injection. You usually check the resultSetHeader.affectedRows to see if there are any lines that have actually changed.
Key points
- Use sql 'UPDATE the SET table column =? WHERE key =?' by connection.execute or connection.query.
- Always use a placeholder to prevent SQL injection.
- The property resultSetHeader.affectedRows tells the number of lines affected.
- Add a specific WHERE clause; without WHERE, the entire table row can change.
- Callback handles error; handles also the case of 0 affected lines (data not found).
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.