Python-MySQL Connectivity
Python-MySQL Connectivity
Connectivity
WHAT IS A DATABASE?
A database is basically a collection of structured data in such a way that it can
easily be retrieved, managed and accessed in various ways. One of the simplest
forms of databases is a text database. Relational databases are the most popular
database system which includes the following:
• MySQL
• Oracle Database
• SQL server
• Sybase
• Informix
• IBM db2
• NO SQL
MySQL
SQL (Structured Query Language) is
a standard language for relational
databases that allow users to do
various operations on data like,
Manipulating, Creating, Dropping,
etc. it supports tools for avoiding
redundant data and ensures integrity
of data. Python supports DDL (Data
Definition Language) and DML (Data
Manipulation Language) and Query
processing. For database
programming, the Python DB-API is
the commonly used module that
provides a database application
programming interface.
2
Python-MySQL An application stores a large amount of data in the for
which is not directly visible/accessible by the user. Thi
Connectivity to give suitable responses, retrieving information etc. t
called Back-End Database
For designing real life applications, we
need to store information or data to the SQL Commands like create, update, alter, insert, delet
memory. For usual example programs we DDL Commands can be implemented using the python
accept data from the user process it and
display some output. But these data are
Why Python?
not stored, thus when we run the
program again, we need a new set of There are several advantages are there for integrating
data. This drawback can be overcome by Python is:
integrating python interface with a • Efficient and Faster
database. Thereby fetching the data • Portable
whenever necessary. • Platform-Independent
• Support for SQL Cursors
• Connection Management
Front End & Back End • Support for RDBMS
The input data is fetched/captured from • Compatibility with Multiple database systems
the user using the python interface. Thus,
in a database programming application
python is the Front-End Interface of
the application
3
Installing MySQL-Connector
To establish connectivity between python and MySQL, Python Database
Interfaces and API (Application Programming Interface) s.
A Database API process includes
• Importing API Module
• Establishing connection with a database
• Performing SQL Statements
• Closing Connection
Establishing Connection
To establish connection between MySQL dB and Python as the first step we
have to import the mysql.connector module. in this step we are importing
the installed mysql connector to python script.
import mysql.connector
Cursor Object
As the third step, to use the connection created there must have to be a
cursor object. It allows us to operate in multiple environments using a single
connection.
Cursors are created by the connection.cursor() method. Once the cursor is
created it lasts till the session is wrapped.
4
Establishing Connection
1. Use the mysql.connector.connect() method to create the
connection object with the required parameters (host, user, paswrd).
mydb=mysql.connector.connect(host=’*’,user=’*’,passwd=’*’)
2. Use the connection object to create the cursor object
mycursor=mydb.cursor()
3. Use cursor.execute() to execute MySQL queries.
mycursor.execute(“SELECT * FROM SCHOOL”)
4. After work use connection.close() to close the connection with
MySQL.
5. Use Exception to catch if any arise during the program.
Connection Arguments
• Username: Username of MySQL server. Default value is “root”.
• Password: given during the time of installation. If user is root
password is not necessary.
• Hostname: Server name or IP address of the system.
5
Python MYSQL Database Connection
After executing each query, we can further check or verify the command
have been executed or not using the MySQL command prompt using the
SQL queries.
6
Implementing DDL Commands
Using the important DDL commands we can modify the already created
table, insert data into the table etc. the python script.
Example:
7
Inserting and Deleting Data
Insertion of data to a table is done with the command INSERT INTO, and
deleting rows from a table is done using the DELETE FROM <table_name>.
Similarly UPDATE command is used to update one or more records on the
table.
Closing the connection
A database can only handle a limited number of connections at a time. To
close a connection that established using the connector object
cursor.close() method is used. It is ideal to close the connection after
finishing the operations on the database.