{"id":5763,"date":"2017-06-15T00:14:11","date_gmt":"2017-06-15T07:14:11","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5763"},"modified":"2024-01-03T17:50:44","modified_gmt":"2024-01-04T00:50:44","slug":"insert","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/","title":{"rendered":"Inserting Rows into a Table from Node.js"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.<\/p>\n\n\n\n<p class=\"note\">This tutorial picks up where the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\">Connecting to MySQL server from a Node.js application<\/a> tutorial left off.<\/p>\n\n\n\n<p>To insert a new row into a table, you follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First,<a href=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\"> connect to the MySQL database.<\/a><\/li>\n\n\n\n<li>Second, execute an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\"><code>INSERT<\/code><\/a> statement by calling the <code>query()<\/code> method on a <code>connection<\/code> object.<\/li>\n\n\n\n<li>Third, close the database connection.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Insert one row into a table<\/h2>\n\n\n\n<p>Create a new <code>insert.js<\/code> file in the project directory and add the following code to the file to insert a new row into the <code>todos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);\n\n<span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});\n\nconnection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-comment\">\/\/ insert statment<\/span>\n  <span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">`INSERT INTO todos(title,completed)\n           VALUES('Learn how to insert a new row',true)`<\/span>;\n\n  <span class=\"hljs-comment\">\/\/ execute the insert statment<\/span>\n  connection.query(sql);\n\n  <span class=\"hljs-comment\">\/\/ close the database connection<\/span>\n  connection.end();\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, use the <code>mysql<\/code> module to connect to the MySQL server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a connection object with the information provided in the <code>.env<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>.env<\/code> file looks like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">DB_HOST=localhost\nDB_PORT=3306\nDB_USER=user\nDB_PASSWORD=password\nDB_NAME=todoapp<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you need to replace the user and password with the ones you use to connect to the MySQL server.<\/p>\n\n\n\n<p>Third, connect to the MySQL server using the <code>connect()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-comment\">\/\/ ...<\/span>\n\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, construct the <code>insert<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">`INSERT INTO todos(title,completed)\n           VALUES('Learn how to insert a new row',true)`<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, execute the <code>INSERT<\/code> statement using the <code>query()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.query(sql);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, close the database connection:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.end();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To run the <code>insert.js<\/code> program, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">node --env-file .env insert.js<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Verify the insert<\/h2>\n\n\n\n<p>First, connect to the MySQL server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">mysql -u root -p<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, retrieve data from the <code>todos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> todos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+-------------------------------+-----------+\n| id | title                         | completed |\n+----+-------------------------------+-----------+\n|  <span class=\"hljs-number\">1<\/span> | Learn how to insert a <span class=\"hljs-keyword\">new<\/span> row |         <span class=\"hljs-number\">1<\/span> |\n+----+-------------------------------+-----------+\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates that the program has inserted a new row into the <code>todos<\/code> table successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting a row and returning the inserted id<\/h2>\n\n\n\n<p>Create a new file <code>insert_return.js<\/code> and add the following code to the file to insert a new row into the <code>todos<\/code> table and returns the inserted ID:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);\n\n<span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});\n\nconnection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-comment\">\/\/ insert statment<\/span>\n  <span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">`INSERT INTO todos(title,completed)\n             VALUES(?,?)`<\/span>;\n\n  <span class=\"hljs-keyword\">let<\/span> todo = &#91;<span class=\"hljs-string\">'Insert a new row with placeholders'<\/span>, <span class=\"hljs-literal\">false<\/span>];\n\n  <span class=\"hljs-comment\">\/\/ execute the insert statment<\/span>\n  connection.query(sql, todo, (err, results, fields) =&gt; {\n    <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n    <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Todo Id:'<\/span> + results.insertId);\n  });\n\n  <span class=\"hljs-comment\">\/\/ close the database connection<\/span>\n  connection.end();\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, construct the query that uses question marks (<code>?<\/code>) as the placeholders:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">`INSERT INTO todos(title,completed)\n           VALUES(?,?)`<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, execute the query and pass data to it in the <code>query()<\/code> method and display the inserted ID if the query succeeded:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.query(sql, todo, (err, results, fields) =&gt; {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Todo Id:'<\/span> + results.insertId);\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Execute the <code>insert_return.js<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">node --env-file .env insert_return.js<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Todo Id:<span class=\"hljs-number\">2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Insert multiple rows<\/h2>\n\n\n\n<p>Create a new file called <code>insert_multiple.js<\/code> and use the following code to insert multiple rows into the <code>todos<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);\n\n<span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});\n\nconnection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-comment\">\/\/ insert statment<\/span>\n  <span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">'INSERT INTO todos(title, completed) VALUES ?'<\/span>;\n\n  <span class=\"hljs-keyword\">let<\/span> todos = &#91;\n    &#91;<span class=\"hljs-string\">'Master Node.js MySQL'<\/span>, <span class=\"hljs-literal\">false<\/span>],\n    &#91;<span class=\"hljs-string\">'Build Node.js \/ MySQL App'<\/span>, <span class=\"hljs-literal\">true<\/span>],\n  ];\n\n  <span class=\"hljs-comment\">\/\/ execute the insert statment<\/span>\n  connection.query(sql, &#91;todos], (err, results, fields) =&gt; {\n    <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n    <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">`Inserted Rows: <span class=\"hljs-subst\">${results.affectedRows}<\/span>`<\/span>);\n  });\n\n  <span class=\"hljs-comment\">\/\/ close the database connection<\/span>\n  connection.end();\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, construct the <code>INSERT<\/code> statement with one question mark(<code>?<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> sql = <span class=\"hljs-string\">`INSERT INTO todos(title, completed) VALUES ?`<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The question mark (?) in the <code>INSERT<\/code> statement represents multiple-row data that will come from an array.<\/p>\n\n\n\n<p>Second, prepare data for insertion:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> todos = &#91;\n    &#91;<span class=\"hljs-string\">'Master Node.js MySQL'<\/span>, <span class=\"hljs-literal\">false<\/span>],\n    &#91;<span class=\"hljs-string\">'Build Node.js \/ MySQL App'<\/span>, <span class=\"hljs-literal\">true<\/span>],\n];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, execute the <code>INSERT<\/code> statement with the data that comes from the <code>todos<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.query(sql, &#91;todos], (err, results, fields) =&gt; {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Row inserted:'<\/span> + results.affectedRows);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can access the number of rows inserted via the <code>affectedRows<\/code> property of the results object.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">node --env-file .env insert_multiple.js<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Inserted Rows: 2<\/code><\/span><\/pre>\n\n\n<p>The output indicates that two rows were inserted.<\/p>\n\n\n\n<p>In this tutorial, you have learned how to insert one or more rows into a MySQL table from a Node.js program.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5763\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/\"\n\t\t\t\tdata-post-title=\"Inserting Rows into a Table from Node.js\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5763\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/\"\n\t\t\t\tdata-post-title=\"Inserting Rows into a Table from Node.js\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to insert one or more rows into a table from the node.js application.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":5759,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5763","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Inserting Rows Into a Table from Node.js<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inserting Rows Into a Table from Node.js\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-04T00:50:44+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/insert\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/insert\\\/\",\"name\":\"Inserting Rows Into a Table from Node.js\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2017-06-15T07:14:11+00:00\",\"dateModified\":\"2024-01-04T00:50:44+00:00\",\"description\":\"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/insert\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/insert\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/insert\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Node.js\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Inserting Rows into a Table from Node.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inserting Rows Into a Table from Node.js","description":"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/","og_locale":"en_US","og_type":"article","og_title":"Inserting Rows Into a Table from Node.js","og_description":"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-04T00:50:44+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/","url":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/","name":"Inserting Rows Into a Table from Node.js","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2017-06-15T07:14:11+00:00","dateModified":"2024-01-04T00:50:44+00:00","description":"In this tutorial, you will learn how to insert one or more rows into a table from the Node.js application.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/insert\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Node.js","item":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/"},{"@type":"ListItem","position":3,"name":"Inserting Rows into a Table from Node.js"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=5763"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5763\/revisions"}],"predecessor-version":[{"id":14086,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5763\/revisions\/14086"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5759"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=5763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}