Introduction To Database Programming in Python
Introduction To Database Programming in Python
Introduction to Database
Programming in Python
1
© NIELIT Gorakhpur Centre.
Introduction to Database Programming in Python
The Python programming language has powerful features for database
programming.
Python supports various databases like MySQL, Oracle, Microsoft SQL Server,Sybase,
PostgreSQL, MangoDB 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.
There are many good reasons to use Python for programming database applications:
Programming in Python is arguably more efficient and faster compared to other
languages.
Python is famous for its portability.
It is platform independent.
Python supports SQL cursors.
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.
For MySQL
For ODBC
Databases are useful for storing information categorically. A company may have a database
with the following tables:
•Employees
•Products
•Customers
•Orders
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL
Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables. A table is a collection of
related data entries and it consists of columns and rows.
To add column:
ALTER TABLE table_name
ADD column_name datatype;
To delete column:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
SELECT MIN(column_name)
FROM table_name
WHERE condition; SELECT AVG(column_name)
FROM table_name
SELECT MAX(column_name) WHERE condition;
FROM table_name
WHERE condition; SELECT SUM(column_name)
FROM table_name
SELECT COUNT(column_name) WHERE condition;
FROM table_name
WHERE condition;
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
• Inner Join
• Left Join
• Right Join
• Full Outer Join
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
EmployeeDB
Create following table:
• employee (emp_id, emp_name, emp_fname, emp_dob,
emp_city, emp_mobile, emp_gender, emp_salary,
dept_id)