Node.js MySQL Insert Into
π Node.js MySQL β INSERT INTO
To insert data into MySQL using Node.js:
-
Connect to the database
-
Use an SQL
INSERTstatement -
Execute using
connection.query()orconnection.execute()(prepared statement)
β 1. Install mysql2
β 2. Connect to MySQL Database
Example: Using database mydb
β 3. Insert a Single Record
insert_one.js
π Output Example
β 4. Insert Multiple Records
β 5. Insert with Prepared Statements (Recommended)
Protects from SQL injection.
β 6. Insert Using Async/Await (Best Way)
(Using mysql2 promise API)
insert_async.js
π 7. Insert with Currents Timestamp Auto
If your table uses:
Then you donβt need to insert the date β it fills automatically.
π― 8. Checking Inserted Data
After insert, you can check:
Or create a small Node query to fetch data.
