Node.js MySQL Table Creation
In this post, we will discuss how to create a table in XAMPP Server using Node.js Script.
It is important to install mysql package in node.js
Command to install the mysql package:
Copied
npm install mysql
Steps:
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 to create a table in XAMPP. It will take two parameetrs. The first parameter is the SQL Query and the second parameter will handle the result.
- Now type the following command in your command prompt to run the script. node file_name.js
- You can verify whether the table is created or not in XAMPP SERver by using the URL in a browser: http://localhost/phpmyadmin
Copied
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: ""
});
Copied
connection_data.connect(function(err) {
var sql = "CREATE TABLE table_name (column_name datatype,....)";
connection_data.query(sql, function (err, result) {
console.log("Table successfully created");
});
});
TABLE Creation Example 1:-
Let's create a table named village in the facility database that have 3 columns.
Copied
// 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(err) {
// Create a table with three columns in facility database.
//Here columns are village_name,district and people.
var sql = "CREATE TABLE village (village_name VARCHAR(255),
distcict VARCHAR(255), people int)";
connection_data.query(sql, function (err, result) {
console.log("Table-village successfully created");
});
});
Output:
Copied
Table-village successfully created
http://localhost/phpmyadmin/sql.php?db=facility&table=village/pos=0
TABLE Creation Example 2:-
Copied
// 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(err) {
// Create a table with three columns in facility database.
//Here columns are village_name,field_type.
var sql = "CREATE TABLE field (village_name VARCHAR(255),
field_type varchar(255))";
connection_data.query(sql, function (err, result) {
console.log("Table-field successfully created");
});
});
Output:
Copied
Table-field successfully created
You can see that the table- field is created. Now let's check in XAMPP Server. - http://localhost/phpmyadmin/sql.php?db=facility&table=field&pos=0
SummarySo we seen how to create a table in XAMPP Server through Node.js script with two examples. make sure that you have to install mysql package and xampp server.