Professional Documents
Culture Documents
Unit V
Unit V
Database Programming
Introduction
What is database programming?
• A database program is the heart of a business
information system and provides file creation,
data entry, update, query and reporting
functions.
• The traditional term for database software is
"database management system“
Advantages of Database Programming
with Python
• Platform-independent.
• Faster and more efficient.
• Portable.
• Support for relational database systems.
• Easy to migrate and port database application
interfaces.
• Support for SQL cursors.
• It handles open and closed connections.
• Python supports various databases
like MySQL, Oracle, Sybase, PostgreSQL, etc.
• 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.
Syntax for database connection
• import mysql.connector.
#Create the connection object.
• myconn = mysql.connector.connect(host =
"localhost", user = "root",passwd = "google",
database = "mydb")
#printing the connection object.
• print(myconn)
#creating the cursor object.
• cur = myconn.cursor()
• print(cur)
How to connect MySQL database in
Python
• Install MySQL connector module. Use the pip
command to install MySQL connector Python.
• Import MySQL connector module.
• Use the connect() method.
• Use the cursor() method.
• Use the execute() method.
• Extract result using fetchall()
• Close cursor and connection objects.
Mostly used different Types of
Databases
• The Oracle. Oracle is the most widely used commercial
relational database management system, built-in
assembly languages such as C, C++, and Java. ...
• MySQL.
• MS SQL Server.
• PostgreSQL.
• MongoDB.
• IBM DB2.
• Redis.
• Elasticsearch.
What are the types of databases?
• Relational databases.
• NoSQL databases.
• Cloud databases.
• Columnar databases.
• Wide column databases.
• Object-oriented databases.
• Key-value databases.
• Hierarchical databases.
Python Database Application
Programmer’s Interface (DB-API)
What is Python DB API?
• The standard for accessing a database in
Python is the Python DB-API.
• This specifies a set of standard interfaces for
modules that wish to allow Python to access a
specific database.
• There are modules available for most common
databases such as MySQL, Oracle, Microsoft
SQL Server etc.
Error and exception handling in DB API