Professional Documents
Culture Documents
Python-MySQL Connectivity
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 form
which is not directly visible/accessible by the user. Thi
Connectivity to give suitable responses, retrieving information etc. th
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, delete
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
There are several advantages are there for integrating p
again, we need a new set of data. This
Python is:
drawback can be overcome by integrating
python interface with a database. Thereby Efficient and Faster
fetching the data whenever necessary. Portable
Platform-Independent
Support for SQL Cursors
Front End & Back End Connection Management
Support for RDBMS
The input data is fetched/captured from
the user using the python interface. Thus, Compatibility with Multiple database systems
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.
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
Methods to manage MySQL Transactions
Python-MySQL connector has the following methods to facilitate database
transactions;
commit: mysql.connection.commit() method commits the current
transaction with the MySQL server.
Syntax: connection.commit()
rollback: mysql.connection.rollback() reverts or undo the changes
made in the current transaction.
Syntax: connection.rollback()
autocommit: to enable or disable the auto-commit feature