Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

INTRODUCTION

Connecting Python application with MySQL


1. Every application required data to be stored for future reference
to manipulate data. Today every application stores data in
database for this purpose.
2. Python allows us to connect all types of database like Oracle, SQL
Server, MySQL.
3. Before we connect python program with any database like
MySQL we need to build a bridge to connect Python and MySQL.
To build this bridge so that data can travel both ways we need a
connector called “mysql.connector”.
We can install “mysql.connector” by using following methods:
At command prompt (Administrator login)
Type “pip install mysql.connector” and press enter
(internet connection in required)
This connector will work only for MySQL 5.7.3 or later
Or open “https://dev.mysql.com/downloads/connector/python/”
and download connector as per OS and Python version
4. Once the connector is installed you are ready to connect your
python program to MySQL.
The following steps to follow while connecting your python
program with MySQL
Open python
Import the package required (import mysql.connector)
Open the connection to databaseCreate a cursor instance
Execute the query and store it in result-set
Extract data from result-set
Clean up the environment
5. Importing mysql.connector
import mysql.connector
Or
import mysql.connector as ms
Here “ms” is an alias, so every time we can use “ms” in place of
“mysql.connector”
6. Open a connection to MySQL Database
To create connection, connect() function is used
Its syntax is:
o ¤ connect(host=<server_name>,user=<user_name>,
o passwd=<password>[,database=<database>])
Here server_name means database servername, generally it is
given as “localhost”
User_name means user by which we connect with mysql
generally it is given as “root”
Password is the password of user “root”
Database is the name of database whose data(table) we want to
use
7. Creating Cursor
It is a useful control structure of database connectivity.
When we fire a query to database, it is executed and result-set
(set of records) is sent over he connection in one go.
We may want to access data one row at a time, but query
processing cannot happens as one row at a time, so cursor help us in
performing this task. Cursor stores all the data as a
temporary container of returned data and we can fetch data one
row at a time from Cursor.
8. Creating Cursor and Executing Query
TO CREATE CURSOR
Cursor_name = connectionObject.cursor()
For e.g.
mycursor = mycon.cursor()
TO EXECUTE QUERY
We use execute() function to send query to connection
Cursor_name.execute(query)
For e.g.
mycursor.execute(‘select * from emp’)
ABOUT THE PROJECT
The topic of the project is “RAILWAY RESERVATION
SYSTEM”. The railway reservation system enable
passenger to enquiry about the train available on the
basis of source and destination , bookings and
cancellation of ticket etc. This project is a computerised
system for bookings or reserving seats in advance.
Online reservation has made the process of reservation
of seats very much easier than earlier

SOFTWARE REQUIREMENTS:-
❖Python 3.8.10 or higher
❖Spread Sheet Software
❖Windows 7 or higher
❖MS Word (Any after 2007)
❖MS Excel (Any after 2007)

HARDWARE REQUIREMENTS:-
❖Keyboard and Mouse for input
❖Hard Disk 500 GB or more
❖4GB RAM or higher
❖Processor – Intel Core to i3 or higher

Modules used:
i.
Python-matplotlib.pyplot: Matplotlib is an
amazing visualization library in python for 2D
plots of array. Matplotlib is a multiplatform data
visualization library built on NumPy array and
designed to work with the border SciPy stacks. It
was introduced by John Hunter in year 2002.
ii.
Python Pandas: Python Pandas is Python
library for data analysis. Pandas have derived its
name from “Panel data system’’, which is an Eco
metrics term for multi-dimensional, structured
data sets. Today, pandas have become a popular
choice for data analysis.

iii.
csv files: A comma-separated
values (CSV) file is a delimited text file that uses
a comma to separate values. Each line of the file is a
data record. Each record consists of one or more fields,
separated by commas. The use of the comma as a field
separator is the source of the name for this file format.
A CSV file typically stores tabular data (numbers and
text) in plain text, in which case each line will have the
same number of fields.
The CSV file format is not fully standardized. The basic
idea of separating fields with a comma is clear, but the
situation gets complicated when field data also contain
commas or embedded line breaks. CSV
implementations may not handle such field data, or
they may use quotation marks to surround the field.
Quotation does not solve everything: some fields may
need embedded quotation marks, so a CSV
implementation may include escape characters or
escape sequences.

You might also like