Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5891

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T01:29:07+00:00 2024-11-27T01:29:07+00:00

How to create a database in MYSQL using Nodejs

  • 60k

Node.js can be used in database applications. And one of the most popular databases is MySQL.

To enable the code examples that will be explained in this tutorial, you must have MYSQL installed on your computer. Click here to download MYSQL database for free.

Installing MYSQL Driver

Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the “mysql” module, downloaded from NPM.

To download and install the “mysql” module, open the Command Terminal and execute the following code: npm install mysql

MYSQL database driver has been downloaded and installed on your computer.

Node.js can use this module to manipulate the MySQL database

Code Sample:

var mysql = require('mysql');

Creating Connection in Nodejs with MYSQL database

Now, let's start by creating a connection to the database

Note: Always use the username and password from your MySQL database, when connecting to a MYSQL database.

Code Sample:

const mysql = require('mysql');  const con = mysql.createConnection({   host: "localhost",   user: "yourusername",   password: "yourpassword" });  con.connect(function(err) {   if (err) throw err;   console.log("Connected!"); }); 
Enter fullscreen mode Exit fullscreen mode

Now, save the code above in a file called “demo_db_connection.js” and run the file with this command in your terminal:
node demo_db_connection.js

The code sample above will result to:

Connected! 
Enter fullscreen mode Exit fullscreen mode

After that, you can now start querying the database using SQL statements.

How to Query a MYSQL Database in Nodejs

Use SQL statements to read from (or write to) a MySQL database. This is also called “to query” the database.

Note that, the connection object created in the example above, has a method for querying the database.

Code Sample:

con.connect(function(err) {   if (err) throw err;   console.log("Connected!");   con.query(sql, function (err, result) {     if (err) throw err;     console.log("Result: " + result);   }); }); 
Enter fullscreen mode Exit fullscreen mode

The query method takes a sql statements as a parameter and returns the result.

How to create a database in MYSQL with Nodejs

To create a database in MySQL, use the “CREATE DATABASE” statement.

Code Sample:

const mysql = require('mysql');  const con = mysql.createConnection({   host: "localhost",   user: "yourusername",   password: "yourpassword" });  con.connect(function(err) {   if (err) throw err;   console.log("Connected!");   con.query("CREATE DATABASE mydb", function (err, result) {     if (err) throw err;     console.log("Database created");   }); }); 
Enter fullscreen mode Exit fullscreen mode

Now, save the code above in a file called “demo_create_db.js” and run the file in your terminal with the command:
node demo_create_db.js

This will display the result below:

Connected! Database created 
Enter fullscreen mode Exit fullscreen mode

Thanks for reading…
I believe you gain some knowledge.

Happy Coding!

beginnersjavascripttutorialwebdev
  • 0 0 Answers
  • 7 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.