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

UNIT-5

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

• Exception handling is the very easy in python


DB API module
• Python has various option to handling like
warnings, Interface errors, Database errors,
and other programming errors.
Types of errors in Python DB API
• Integrity Error
It means if we enter the duplicate data in
database so it shows integrity error
• Operational Error
• Programming Error
Example sql queries
• Create Table:
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Create Table example sql query

• Here I am used Oracle database for creating


table. So type in google oracle live sql then sign
in

• CREATE TABLE customers ( customer_id int NOT


NULL, customer_name char(50) NOT NULL,
address char(50), city char(50), state char(25),
zip_code char(10) );
Table is created
Inserting Data into table
• insert into customers ( customer_id,
customer_name, address, city,state,
zip_code) values (123,' Naresh U', 'B.N Reddy',
'Hyderabad', 'Telangana', 500070 );
Inserting Data into created Table
Display inserted data in created table
• Select * from customers;
Inserting more rows into table
• By using same sql query with changing of
values we can insert the more rows data into
table
• insert into customers ( customer_id,
customer_name, address, city,state,
zip_code) values (543,'Suresh', 'R.N Reddy',
'Hyderabad', 'Telangana', 500090 );
• insert into customers ( customer_id,
customer_name, address, city,state,
zip_code) values (543,'Venu', 'Ammapalem',
'Khammam', 'Telangana', 507305 );
• insert into customers ( customer_id,
customer_name, address, city,state,
zip_code) values (543,'vamshi', 'paleru',
'Khammam', 'Telangana', 507205 );
Now check the table whether it is
created or not?
select * from customers;
Now table updating using update
command

UPDATE CUSTOMERS SET ADDRESS = 'Pune'


WHERE customer_id = 123;
Whether Really table is updated?
• Select * from customers;
Deleting row from created table!
• DELETE FROM customers WHERE CUSTOMER_ID = 123;
Whether row is deleted or not?
• select * from customers;
Deleting entire table!!!
• TRUNCATE TABLE customers;
Whether table is deleted or not?
select * from customers;
Task is to create following table!!!
Table name is Employee
What is the database programming
code?
Cursor Objects
• A cursor is an object which helps to execute
the query and fetch the records from the
database.
• The cursor plays a very important role in
executing the query.
• It is an object that is used to make the
connection for executing SQL queries
• It acts as middleware between SQLite database
connection and SQL query.
• It is created after giving connection to SQLite
database
• How does cursor work in python?
• Allows Python code to execute PostgreSQL
command in a database session.
• Cursors are created by the connection.
• cursor() method: they are bound to the
connection for the entire lifetime and all the
commands are executed in the context of the
database session wrapped by the connection.
There are several cursor classes in
Mysql db.cursors
• 1. Base cursor
It’s a base class for cursor object
• 2. Cursor is the default cursor class
Cursor warning Result Mix In, Cursor Store Result Mix
In, Cursor tuple Result Mix In, base cursors are the
default cursor classes
• 3. Cursor Store Result Mix In
This is a MixIn class which causes the entire result set
to be stored on the client side, i.e. it uses
mysql_store_result().
• 4. Cursor use Result Mix In
Cursor Store Result Mix In uses the mysql_use_result()
function to retrieve result set from the executed query
Object Relational Managers
• ORMs provide a high-level abstraction upon a
relational database that allows a developer to
write Python code instead of SQL to create,
read, update and delete data and schemas in
their database
• Which is object relational database
management system?
Which is object relational database
management system?
• An object–relational database (ORD), or
object–relational database management
system (ORDBMS), is a database management
system (DBMS) similar to a relational
database, but with an object-oriented
database model
• In this model objects, classes and inheritance
are directly supported in database schemas
and in the query language.
Example

You might also like