Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Verify that npm is already installed by running the command npm -v on the terminal. If npm is installed, then the version number will

Verify that npm is already installed by running the command npm -v on the terminal. If npm is installed, then mysqlConnect.end(function(err) { if (err) { $9 $4 $4 $4 $4 $4 57 } return console.log('error: + err.message); To manage a database from within a .js file, you need first to create a string that holds the SQL command and

Verify that npm is already installed by running the command npm -v on the terminal. If npm is installed, then the version number will be displayed. Otherwise install npm from the Nodejs.org site Create a new folder called mysql Project and make it your working directory. Use the following command to initialize your project directory and create the package.json file. Use -y option to accept the defaults. npm init -y The init command usually invokes a set of questions to customize the package.json to your project. For our purpose we will accept the default values. Install the MySQL driver using the following command npm install mysql2 Note that we will use mysq12 instead of mysql due to an error received when using mysql. If curious, you can learn more about this error here In Vscode open the mysql Project folder and create the file connect.js. In connect.js write the following code. For the user and password write your username and password that you have chosen when setting up the MySQL server 1 2 3 4 567 8 9 10 11 12 13 14 15 //import the mysql2 module const mysql = require ('mysql2'); // initialize the connection object const mysqlConnect = mysql.createConnection({ host "localhost", user : "root", password: "adminPass", }); //Check the status of the connection mysqlConnect.connect((err) =>{ }); if (err) throw err; console.log("Connected!!") Save the file and run it using node connect.js. You should get the following output Connected!! To close a database connection gracefully, you call the end () method on the connection object. The end () method ensures that all remaining queries are always executed before the database connection is closed. Stop the script and add the following code to connect.is, mysqlConnect.end(function(err) { if (err) { $9 $4 $4 $4 $4 $4 57 } return console.log('error: + err.message); console.log("Closing the database connection...'); }); Save the file and run it again using node connect.js. You should get the following output Connected!! Activity Creating a Database in Nodejs In this Activity we will write the code for creating and managing a database from within a .js file executed on the Nodejs platform. You will be given guidance on how to write the code. However, you will be required to fill in the SQL commands relevant to each step. This activity is based on the online MySQL NodeJS tutorial. The database used in this activity is like the database created in Programming Activity 4. However, S_code and C_code are assigned by the user rather than by the DB engine. Closing the database connection... Submit your file to BB. Student Course S code C code SI C4 $2 C4 S8 C4 $3 $3 $3 $5 $5 $5 $6 $6 $6 $9 $9 CI C3 C5 CI C3 C5 CI C3 C5 CI C3 C5 CI C2 C3 C4 C5 C2 grade A B A A A A B A C C B A B B A A B B A B Course C code C_name convenience. 3. Creating the Database: CI C2 C3 C4 CS Student S code S_name S_year $1 Anne 1st S2 Bob 1st Peter 2nd Tracy 3rd Tom 2nd S3 S4 S5 S6 S7 S8 $9 Database Modelling Distributed Systems Software Engineering Programming Languages Compilers EERRE Paul 2nd Smith 3rd 1st Mary John 2nd C book Introduction to Databases Advanced Systems Software Engineering Applications From Fortran to Java Introduction to Compilers Cyear 2nd 3rd 2nd 1st 2nd 1. Copy connect.js to connectl.js. 2. Change the name of the database connection object to a shorter name, such as db, for To manage a database from within a .js file, you need first to create a string that holds the SQL command and then call the query() method on the database connection object passing to it as a parameter the string that holds the SQL command and a callback function. a. Declare the string variable sql to hold the SQL command for creating a new database called Stud Tracking. let sql = "Fill in SQL COMMAND TO CREATE A DATABASE"; b. Pass sql and a call back function to the query () function db.query(sql, (err) => { if(err) throw err; console.log('Database Created'); }); c. Save connect1.js and execute it from the command prompt. d. Check that the new database has been created on the MySQL server. e. If you execute connect1.js again you will receive the following error that indicates that the database already exists. Error: Can't create database 'StudTrack'; database exists f. To avoid this error, add the clause IF NOT EXISTS to the sql command creating the database. Check the MySQL Cheat sheet for the correct syntax. Submit your file to BB. Creating the Database Table: The code for creating the tables in the database will be written after the code for creating the Database developed in the previous step and before the call to the end (). a. Creating the Student Table: assign the string variable sql to hold the SQL command for creating the student table. Note that for the S-code, it is assumed that the student code will be composed of at most 5 characters provided by the user. sql = "Fill in SQL COMMAND TO CREATE Student TABLE"; b. Call the query() function again passing to it the SQL command stored in sql and a callback function as before. Note that you might receive the following errors: i. Error: No database selected. (Hint: you need to specify the database used. One solution is to precede the table name with the database name or check the online manual for another solution) ii. Error: Table 'students' already exists. (Hint: the solution is similar to that used for the database above. Check the online manual for syntax) c. Run your script twice to ensure that you handled all errors d. Create the Course table applying similar steps as before. e. Create StudentCourse table applying similar steps as before.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Answer Steps to follow to achieve target according to question Step 1 Verify that npm is already ins... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting Information Systems

Authors: Marshall B. Romney, Paul J. Steinbart

13th edition

133428532, 978-0133428537

More Books

Students also viewed these Databases questions

Question

What are the main benefits of outsourcing HR?

Answered: 1 week ago