Showing posts with label MySql. Show all posts
Showing posts with label MySql. Show all posts
How to create a PoolCluster in Node.js with MySQL

How to create a PoolCluster in Node.js with MySQL

To create a PoolCluster in Node.js with MySQL, you can follow these steps:

1. Install the mysql package in your Node.js project by running the following command in your terminal:

  npm install mysql
2. Require the mysql module in your Node.js code:
const mysql = require('mysql');
3. Create a new PoolCluster object:
const poolCluster = mysql.createPoolCluster();
4. Add one or more pools to the PoolCluster object:
const poolConfig1 = {
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'database1'
};

const poolConfig2 = {
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'database2'
};

poolCluster.add('MASTER', poolConfig1);
poolCluster.add('SLAVE', poolConfig2);

In this example, we're adding two pools to the PoolCluster object: one for the MASTER database and one for the SLAVE database.

5. Use the PoolCluster object to obtain connections from the appropriate pool:
poolCluster.getConnection('MASTER', (err, connection) => {
  if (err) throw err;

  connection.query('SELECT * FROM my_table', (error, results, fields) => {
    if (error) throw error;

    console.log(results);
    connection.release();
  });
});
In this example, we're obtaining a connection from the MASTER pool and executing a SELECT query on the my_table table.

6. Release the connection back to the pool when you're done with it:
connection.release();
That's it! You now have a working PoolCluster object that you can use to manage connections to multiple MySQL databases in your Node.js application.

How to import the Excel sheet data into MySql database

Hi, In this tutorial, I am going to explain  How to import the Excel sheet data into MySql database.

Some of the organizations are uses the Excel sheets to save their data. When they want to upgrade their system with new technologies. Then we need to convert the entire Excel sheets data into SQL format.

For example, consider the one of the major organization is school
In school, so many students records are stored in the Excel sheet. Now we would like to import all those Excel data into SQL. Generally, this can be achieved by using the ODBC connections using any programming language. But it is somewhat difficult.

I will explain How to import the Excel sheet data into MySql database in the simplest way.
There's a simple online tool that can do i.e sqlizer.io
Here You can upload an XLSX file to it, enter a sheet name, cell range, and database table name. Here student.xlsx contains the student's data i.e there in the Sheet1, cell range is A1:E11 and MySql database table name is student.


After providing this information whenever you press the Convert My File button. Then it will generate a CREATE TABLE statement and a bunch of INSERT statements.
CREATE TABLE student (
    `id` INT,
    `fullname` VARCHAR(13) CHARACTER SET utf8,
    `gender` VARCHAR(7) CHARACTER SET utf8,
    `mobilenumber` INT,
    `city` VARCHAR(6) CHARACTER SET utf8
);
INSERT INTO student VALUES (1,'M.Rajkumar','Male',9959950011,'Guntur');
INSERT INTO student VALUES (2,'Ch.Eswar','Male',9959950011,'Guntur');
INSERT INTO student VALUES (3,'M.Srikanth','Male',9959950011,'Guntur');
INSERT INTO student VALUES (4,'Syed. Rehaman','Male',9959950011,'Guntur');
INSERT INTO student VALUES (5,'N. Nagendra','Male',9959950011,'Guntur');
INSERT INTO student VALUES (6,'D.Naveen','Male',9959950011,'Guntur');
INSERT INTO student VALUES (7,'G.Manohar','Male',9959950011,'Guntur');
INSERT INTO student VALUES (8,'P.Mahalaxmi','Fe Male',9959950011,'Guntur');
INSERT INTO student VALUES (9,'V.Vasavi','Fe Male',9959950011,'Guntur');
INSERT INTO student VALUES (10,'P.Swapna','Fe Male',9959950011,'Guntur');
These statements are imported all your data into a MySQL database

Watch Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff