nodejs
  1. nodejs-mysql-insert-record

Node.js MySQL Insert Record

In Node.js, you can use the mysql package to interact with MySQL databases. In this tutorial, we'll discuss how to insert a record into a MySQL database using Node.js.

Syntax

The syntax for inserting a record in a MySQL database using Node.js is as follows:

connection.query('INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)', [value1, value2, value3], function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});

Here, "table_name" is the name of the table to insert the record into, and "column1, column2, column3" are the names of the columns in the table. The question marks (?) are placeholders for the values to be inserted into the columns. The values are passed as an array of arguments to the query() method.

Example

Let's say we have a MySQL database with a table called "employees" which has three columns: "id", "name", and "age". Here's how we can insert a record into the "employees" table using Node.js:

const mysql = require('mysql');

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

const employee = {
  name: 'John Doe',
  age: 30
};

connection.query('INSERT INTO employees SET ?', employee, function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});

connection.end();

Output

When we run the example code above, the output will be the ID of the inserted record:

{ fieldCount: 0,
  affectedRows: 1,
  insertId: 2,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0 }

This is because the query() method returns an object containing information about the inserted record.

Explanation

In the example above, we first created a connection to the MySQL database using the mysql package. We then created an object called "employee" which contains the name and age of the employee we want to insert.

We used the query() method to insert the record into the "employees" table. The SET keyword was used to insert the employee object into the table. We passed the employee object as the second argument to the query() method.

Finally, we closed the connection to the database.

Use

You can use Node.js to insert records into MySQL databases when building web applications, APIs, or any other type of application that interacts with a database.

Important Points

  • Make sure to create a connection to the database before attempting to insert records.
  • Remember to close the connection to the database when you're done.
  • Always sanitize user inputs to prevent SQL injection attacks.

Summary

In this tutorial, we discussed how to insert a record into a MySQL database using Node.js. We covered the syntax, example, output, explanation, use, and important points of Node.js MySQL insert record. With this knowledge, you can now use Node.js and the mysql package to interact with MySQL databases and insert records into them.

Published on: