Kingdom of Saudi Arabia المملكة العربية السعودية
Ministry of Education
وزارة التعليم
University of Tabuk
Applied College جامعة تبوك
Computer Science Department الكلية التطبيقية
قسم علوم الحاسب
ACCS 1207 Programming and Database
Lecture 3
The Python DB-API
Prepared by:
Teacher Assistant: Eman Alofi
Academic Year: 2nd Semester 1445
Outlines
• In this lecture, students should be able to learn:
• Python Database-DB
• Adopters
• The layers in Writing Python Database
• Application Program Interface-API
• Use Case Study
• Summary Questions
Introduction
Introduction
• In the next chapters of this course we are going to:
• Cover the Python database API – Application Program Interface
• Look at how to access relational databases from Python
• Either directly through a database interface,
• Or via an ORM—Object Relational Mapping and how you can
accomplish the same task but without necessarily having to give
explicit commands in SQL.
Python
Database - DB
Part1
Python Database – DB, Adopter
• Python’s standard database interface is Python DB-API.
• This interface uses the MySQLdb module for only MySQL.
• This module is independent of any other database engine, so we
need to write Python scripts to access any other database engine.
• The way to access a database from Python is via an adapter.
• An adapter is a Python module with which you can connect
interface to a relational database’s client library.
Connect to Python Database – DB in Python Example
The layers in Writing Python Database
• The figure illustrates the layers involved in writing a Python
database application, with and without an ORM. The figure
demonstrates that the DB-API is your interface to the C libraries of
the database client.
API
Part2
Application Program Interface- API
• The API is a specification that states a set of required objects and
database access mechanisms to provide consistent access across the
various database adapters and underlying database systems.
• API has been defined to encourage similarity between the Python
modules that are used to access databases.
API helps achieving consistency leading to more easily understood
modules, code that is generally more portable across databases, and a
broader reach of database connectivity from Python.
Application Program Interface- API- Example
Use Case Study
Part3
Use Case Study – Step 1
• First we have to have a table that is has peoples’ data. This database is
called “Person”
Use Case Study – Step 2
• Secondly we have to connect to the database by using Python Programing
Language via an adopter “connect”.
Use Case Study – Step 3
• Third, we are writing a queries directly from Python
Use Case Study – Step 3
Use Case Study Note
• We are going to complete the use case study case as we cover the next
lectures.
Summary Questions
• The way to access a database from Python is via an __
• An adapter is __
• connect() is an example of __
• API stands for __
• API helps achieving consistency in understood modules, code that is
generally more portable across databases, database connectivity from
Python.
❑True
❑False
References
• Core Python Applications Programming by Wesley J Chun
• https://arctype.com/blog/sql-python/
• https://peps.python.org/pep-0249/
https://towardsdatascience.com/how-to-access-relational-
databases-in-python-f711cb38e235