0% found this document useful (0 votes)
5 views10 pages

Database Programming in Python

Uploaded by

Ayush Debbarma
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)
5 views10 pages

Database Programming in Python

Uploaded by

Ayush Debbarma
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/ 10

Database Programming

in python
Introduction:
• To build the real world applications, connecting with the databases is the
necessity for the programming languages. However, python allows us to
connect our application to the databases like MySQL, SQLite, MongoDB,
and many others.
• Python also supports Data Definition Language (DDL), Data Manipulation
Language (DML) and Data Query Statements. For database programming,
the Python DB-API is a widely used module that provides a database
application programming interface.

The Python Programming language has powerful features for database


programming, those are

• Python is famous for its portability.


• It is platform independent.
• In many programming languages, the application developer needs to take
care of the open and closed connections of the database, to avoid further
exceptions and errors. In Python, these connections are taken care of.
• Python supports relational database systems.
• Python database APIs are compatible with various databases, so it is very
easy to migrate and port database application interfaces.
Environment Setup:
• In this topic we will discuss Python-MySQL database connectivity,
and we will perform the database operations in python.
• The Python DB API implementation for MySQL is possible by
MySQLdb or mysql.connector.

Note: The Python DB-API implementation for MySQL is possible by


MySQLdb in python2.x but it deprecated in python3.x.In Python3.x,DB
-API implementation for MySQL is possible by mysql.connector.

• You should have MySQL installed on your computer


Windows:
You can download a free MySQL database at
https://www.mysql.com/downloads/.
Linux(Ubuntu):
sudo apt-get install mysql-server
• You need to install MySQLdb: (in case of Python2.x)
• MySQLdb is an interface for connecting to a MySQL database server
from Python. The MySQLdb is not a built-in module, We need to
install it to get it working.
• Execute the following command to install it.
➢ For (Linux)Ubuntu, use the following command -
sudo apt-get install python2.7-mysqldb
➢ For Windows command prompt, use the following command -
pip install MySQL-python
• To test if the installation was successful, or if you already have
"MySQLdb" installed, execute following python statement at
terminal or CMD.
import MySQLdb
• If the above statement was executed with no errors,
"MySQLdb " is installed and ready to be used.
(OR)
• You need to install mysql.connector: (in case of Python3.x)
• To connect the python application with the MySQL database,
we must import the mysql.connector module in the program.
• The mysql.connector is not a built-in module, We need to install it
to get it working.
• Execute the following command to install it using pip installer.
➢ For (Linux)Ubuntu, use the following command -
pip install mysql-connector-python
➢ For Windows command prompt, use the following command -
pip install mysql-connector
• To test if the installation was successful, or if you already have
" mysql.connector " installed, execute following python
statement at terminal or CMD.
import mysql.connector
• If the above statement was executed with no errors,
"mysql.connector " is installed and ready to be used.
Python Database Application Programmer’s Interface (DB-API):

• Python DB-API is independent of any database engine, which


enables you to write Python scripts to access any database engine.
• The Python DB API implementation for MySQL is possible by
MySQLdb or mysql.connector.
• Using Python structure, DB-API provides standard and support for
working with databases.
The API consists of:
1. Import module(mysql.connector or MySQLdb)
2. Create the connection object.
3. Create the cursor object
4. Execute the query
5. Close the connection
1. Import module(mysql.connector or MySQLdb):

• To interact with MySQL database using Python, you need first


to import mysql.connector or MySQLdb module by using
following statement.

• MySQLdb(in python2.x)
import MySQLdb

• mysql.connector(in python3.x)
import mysql.connector
2. Create the connection object:
• After importing mysql.connector or MySQLdb module, we need to create
connection object, for that python DB-API supports one method i.e. connect ()
method.
• It creates connection between MySQL database and Python Application.
• If you import MySQLdb(in python2.x) then we need to use following code to
create connection.
Syntax:
Conn-name=MySQLdb.connect(<hostname>,<username>,<password>,<database>)
Example:
Myconn =MySQLdb.connect ("localhost","root","root",”emp”)
(Or)
• If you import mysql.connector(in python3.x) then we need to use following code
to create connection.
Syntax:
conn-name= mysql.connector.connect (host=<host-name>,
user=<username>,passwd=<pwd>,database=<dbname>)
Example:
myconn=mysql.connector.connect(host="localhost",user="root",
passwd="root",database=”emp”)
3. Create the cursor object:
• After creation of connection object we need to create cursor
object to execute SQL queries in MySQL database.
• The cursor object facilitates us to have multiple separate
working environments through the same connection to the
database.
• The Cursor object can be created by using cursor () method.

Syntax:
cur_came = conn-name.cursor()

Example:
my_cur=myconn.cursor()
4. Execute the query:
• After creation of cursor object we need to execute required
queries by using cursor object. To execute SQL queries,
python DB-API supports following method i.e. execute ().
Syntax:
cur-name.execute(query)
Example:
my_cur.execute (“select * from Employee”)
5. Close the connection:
• After completion of all required queries we need to close the
connection.
Syntax:
conn-name.close()
Example:
conn-name.close()

You might also like