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:
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
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query that uses DEGREES() function.
- Now type the following command in your command prompt to run the script. node file_name.js
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
connection_data.connect(function(error) {
connection_data.query("SELECT DEGREES(column),.... FROM table_name
WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the details table with the following records:
DEGREES Example 1:-
Let's return the degrees from the radians column.
// 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:
[
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
// 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:
[
RowDataPacket {
radians: 3.5,
'DEGREES(radians)': 200.53522829578813
}
]
There ais only one row that satisfy the condition and degree is returned.
SummaryIn this post, we seen how to use DEGREES() function on MySQL table in XAMPP Server with node.js script.