Node.js MySQL - DEGREES()

In this post, we will discuss how to convert radians to degrees using Node.js script in MySQL XAMPP server with DEGREES() function.

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

DEGREES() Function:

This function will convert the given radians to degrees in mysql table. Here we can pass the column that have radians.

Syntax:

DEGREES(column)

Steps for Node.js script:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses DEGREES() function.
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT DEGREES(column),.... FROM table_name 
      WHERE condition/s...", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the details table with the following records:

alt=

DEGREES Example 1:-

Let's return the degrees from the radians column.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {  
  // Write sql query that uses DEGREES
  connection_data.query("SELECT radians, DEGREES(radians) from details;", \
  function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    radians: 1.34,
    'DEGREES(radians)': 76.77634645998329
  },
  RowDataPacket {
    radians: 2.45,
    'DEGREES(radians)': 140.37466253912737
  },
  RowDataPacket { radians: 0, 'DEGREES(radians)': 0 },
  RowDataPacket {
    radians: 3.5,
    'DEGREES(radians)': 200.53522829578813
  }
]

You can see that radians were converted to degrees.

DEGREES Example 2:-

Let's return the degrees from the radians column where radians>3.4

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {  
  // Write sql query that uses DEGREES
  connection_data.query("SELECT radians, DEGREES(radians) from 
  details where radians>3.4;", function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    radians: 3.5,
    'DEGREES(radians)': 200.53522829578813
  }
]

There ais only one row that satisfy the condition and degree is returned.

Summary

In this post, we seen how to use DEGREES() function on MySQL table in XAMPP Server with node.js script.