Node.js MySQL - RADIANS()

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

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

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

RADIANS() Function:

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

Syntax:

RADIANS(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 RADIANS() function.
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT RADIANS(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=

RADIANS Example 1:-

Let's return the radians from the degrees 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 RADIANS
  connection_data.query("SELECT degrees, RADIANS(degrees) from details;", 
  function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { degrees: 360, 'RADIANS(degrees)': 6.283185307179586 },
  RowDataPacket { degrees: 180, 'RADIANS(degrees)': 3.141592653589793 },
  RowDataPacket { degrees: 0, 'RADIANS(degrees)': 0 },
  RowDataPacket { degrees: 270, 'RADIANS(degrees)': 4.71238898038469 }
]

You can see that degrees were converted to radians.

RADIANS Example 2:-

Let's return the radians from the degrees column where degrees=360.

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 RADIANS
  connection_data.query("SELECT degrees, RADIANS(degrees) 
  from details where degrees=360;", function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { degrees: 360, 'RADIANS(degrees)': 6.283185307179586 }
]

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

Summary

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