0% found this document useful (0 votes)
70 views73 pages

Unit 6 - CONNECT TO DATABASE

This document provides instructions for connecting to a MySQL database and visualizing data using Python. It explains how to install MySQL Workbench and Docker to set up a local MySQL database. Steps are provided to download the MySQL image from Docker Hub, run the image as a container, and connect MySQL Workbench to the database. The document also demonstrates how to write Python code to connect to the MySQL database, fetch data from a sample student_record table, and print the results. Key topics covered include SQL vs NoSQL databases, popular SQL databases like MySQL, and using modules like mysql-connector-python to connect Python programs to MySQL.

Uploaded by

Nima Dorji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views73 pages

Unit 6 - CONNECT TO DATABASE

This document provides instructions for connecting to a MySQL database and visualizing data using Python. It explains how to install MySQL Workbench and Docker to set up a local MySQL database. Steps are provided to download the MySQL image from Docker Hub, run the image as a container, and connect MySQL Workbench to the database. The document also demonstrates how to write Python code to connect to the MySQL database, fetch data from a sample student_record table, and print the results. Key topics covered include SQL vs NoSQL databases, popular SQL databases like MySQL, and using modules like mysql-connector-python to connect Python programs to MySQL.

Uploaded by

Nima Dorji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Unit 6: Connect to Database and Visualize Data

CSA202: Data Analytics and Visualization


Ms. Pema Wangmo

Gyalpozhing College of Information Technology


Royal University of Bhutan
BCS Year II, Sem I
Learning objective
● Introduction to SQL and NoSQL
● Install mysql workbench
● What is mysql?
● Install docker
● Install mysql database in docker
● Connect mysql workbench to mysql database
● Run your sql in workbench
● Write python code to connect to mysql database
● Write python code to fetch data from mysql database
SQL vs NoSQL
● In the world of database technologies, there are two main types of databases

SQL NoSQL
Relational DB Non-relational DB
SQL vs NoSQL
● Relational databases are structured, like phone books that store phone numbers

and addresses in a fixed format

● Non-relational databases are non-structured, document-oriented and distributed,

like file folders that hold everything from a person’s address and phone number to

their Facebook likes and online shopping preferences


Relational vs Non-Relational DB
Structured
● Order transaction
Non-structured
● Twitter stream
Popular SQL Databases

MySQL The most popular open-source database

Oracle RDBMS Full-service, enterprise level offering by Oracle Corporation

MS SQL Full-service, enterprise level offering by Microsoft Corporation


Server
Sybase a relational model database server product for businesses primarily
used on the Unix OS, which was the first enterprise-level DBMS for
Linux
PostgreSQL an enterprise-level, object-relational DBMS that uses procedural
languages like Perl and Python, in addition to SQL-level code.
mySQL
What is mySQL?
● MySQL is a publicly accessible Relational Database Management System (RDBMS)

that uses Structured Query language (SQL) to interact with databases.

● MySQL stores data in the form of tables that can be modified using Structured

Query Language.

● Its adaptability with different computing systems like Windows, Linux, macOS,

and Ubuntu has made it an easy-going RDBMS software option.


What is Mysql workbench?
● MySQL Workbench is a unified software used to add functionality and ease to

SQL development work.

● MySQL Workbench provides data modeling, SQL development, and various

administration tools for configuration.

● It also offers a graphical interface to work with the databases in a structured

way.
Steps to install mysql workbench in windows
● Go to the url

https://dev.mysql.com/downloads/workbench/#:~:text=You%20do%20not%20nee

d%20to,0.9.3.tar.gz

● Download mysql workbench for windows or mac accordingly


● Click on no, thanks link and your download will start.
Steps for configuration

1
2

Click on
next
Steps for configuration
Select workbench
to install and click
next

Click on 4
custom
Steps for configuration

5 6
Click on
install
Steps for configuration

Click on finish and


launch mysql workbench
7
Steps for configuration

You have successfully


launched workbench
MySQL database using
docker
How to set up a MySQL database using Docker?
1. Download docker

2. Set up account in docker hub

3. Run docker and download image

4. Start mySQL image

5. Connect to database and run SQL


What is docker?
● Docker is an open source platform for automating the deployment of applications

as portable, self-sufficient containers that can run on the cloud or on-premises.

● Docker enables developers to build, deploy, run, update and manage containers.
Download docker
1. Go the following url and download docker https://www.docker.com/ and install in

your system. Run your docker once it is installed.


Download docker
2. Docker will start the service. The

files that docker uses to run the

applications are called images

which are pre-built collection of

files. These images are available

on a website called Docker hub.


Set up an account in docker hub
● To download the image, we need to set up an

account in docker hub

● Visit the url https://hub.docker.com/ and

create an account.

● Remember the password, you will need it later.


Set up an account in docker hub
● Select the free plan and verify your

email address.
Download mysql image
● Once your email is verified, log

in to docker hub and start

searching for the image you

want
Download mysql image
● You can only download the image after logging into docker hub

● Open your terminal or cmd and run the command to download to mysql

docker pull mysql


Default command to run
● After you finish downloading, the command below is default command to run

mySQL

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d

mysql:tag
Run mysql image
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d

mysql:tag

● e- environment variable

● some-mysql is the name of image you want to assign to your container,

● my-secret-pw is the password to be set for the MySQL root user

● -d: detached mode where the container runs in the background and we can use terminal for other commands

● tag is the tag specifying the MySQL version you want.


Lets make changes to run command
Original command

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d

mysql:tag

Command changed

docker run -p 13306:3306 --name mysql-docker-local –e MYSQL_ROOT_PASSWORD=Password -d

mysql:latest

Note: Change the port number if it gives error


Run mysql image
● Run the command below to check whether the container has been created or not

docker ps
Run mysql image
● You can see inside the docker also, the container name mysql-docker-local has been

created
Connect to mySQL workbench

Now we have docker container running mysql database. Lets connect to it and run some

sql. We will use mySQL workbench as our IDE


Mysql workbench

Click on plus button near

mysql connections to make

the connections
Mysql workbench

● Give any connection name

● Give the password that you have

set while running the mysql

image through your terminal

● Change the port to 13306 (port

number you provided while

running the mysql image)


Mysql workbench

● Click on test connection to

check whether connection has

been made successfully or not

with mysql database.

● Successful message will pop

up if the connection is made.


Mysql workbench

● Click on the connection

you have created


Mysql workbench

● Now you are in your mysql

docker connection

● You can successfully run

the sql code as shown


How to stop the mysql database

● Command: docker stop “image_name”

● Example: docker stop mysql

● This command will stop the container and mysql database.

● OR you can go to docker and stop the container too.


Connect to MySQL database
with python
Steps to connect MySQL database with Python.
1. Install a module name mysql-connector-python which can be installed by using

● mysql-connector-python module of Python is used to connect MySQL databases with the

Python programs or any programming language.

● It uses the Python standard library and has no dependencies.


Steps to connect MySQL database with Python.
2. Import mysql connector
Steps to connect MySQL database with Python.
3. Now we can create a variable say mydb which is used to store the result of the

connection. So we can connect MySQL with Python by using the connect() method

which belongs to mysql.connector class.


Steps to connect MySQL database with Python.

● Port: port number you assigned while running the mysql image

● host which can be your localhost or some other host.

● user which is no other than the username of mysql database.

● password which is your password in mysql database.

● database which is the name of database from which the data is to be fetched.
Create database STUDENT
● Open mysql workbench and create the database by providing the following

command.

● After creating, select the database to fetch the data


Fetch data from MySQL
database
Steps to fetch information from MySQL database
1. In order to fetch information from the database, we create a variable say

mycursor which will store the cursor of the current database.

● A cursor allows you to iterate a set of rows returned by a query and process each

row to get the desired information.


Create student_record table in mysql workbench
● Create student_record table with the following columns and insert the values
Steps to fetch information from MySQL database
2. Student details is already inserted in mysql database and the following table is

how it looks like


Steps to fetch information from MySQL database
3. Now in order to obtain the desired query, we use execute() method of mycursor

which will take SQL query as an argument and also we store the result of the

query by using fetchall of mycursor which can be done as follows:

student_name, marks are the column of


table student_record
Steps to fetch information from MySQL database
4. As you can see in the above query we are trying to fetch Student Name and Student Marks

from the student_record table. Now we store student Name and their respective Marks into

two separate lists so that we can plot them in a bar graph.


How to visualize data using
matplotlib
Visualize data using matplotlib
1. We will use seaborn to plot bar graph for this example
Visualize data using matplotlib
How to import csv file in MySQL
table using mysql workbench?
Steps to import csv file in mysql table using mysql workbench
● Lets take iris dataset “iris.csv” to import in mysql workbench.

1. Create database or use existing database in mysql

● I have created database name called “iris”


Steps to import csv file in mysql table using mysql workbench
2. Right click on table under the database name iris. Click on “table data import

wizard”
Steps to import csv file in mysql table using mysql workbench
3. Select the dataset which you

want to do visualization for.


Steps to import csv file in mysql table using mysql workbench
4. Create a new table or use any

existing table relevant to the

dataset. I have created the

new table called “Iris”. Then

click on next.
Steps to import csv file in mysql table using mysql workbench
5. Configuration set up box will

pop up.
Steps to import csv file in mysql table using mysql workbench
6. Click on next button which

will import the dataset.


Steps to import csv file in mysql table using mysql workbench
● Data will start importing

from the file


Steps to import csv file in mysql table using mysql workbench
7. Your dataset has been

finished importing and your

table has been created. Click

on finish button now.


● This is how your dataset

looks like when it is finally

imported in the mysql

workbench.
How to visualize the csv file
data using seaborn, matplotlib?
Steps to visualize csv file
1. After you have finish importing the csv file, connect mysql database with python

using mysql connector.

Change database
name to iris
Steps to visualize csv file
2. Select the required field for the visualization
Steps to visualize csv file
3. Then I have converted the required field into dataframe.
Steps to visualize csv file
4. Import the necessary visualization package
Steps to visualize csv file
any questions??
Find me at [email protected]

You might also like