Lecture 9-Database access
Lecture 9-Database access
Python Programming
by Gideon T. Marandu
Ardhi University
Introduction
• Interacting with databases is a common task in Python.
• SQLite simply stores all the data in a single file and allows us to access
that data using SQL syntax.
Using sqlite3 Connects to a local file named
mydb.db.
• Broad Applicability: Similar APIs exist for PostgreSQL, MySQL, Oracle, and other databases,
enabling uniform coding practices.
• DBAPI2 Specification: Database APIs following DBAPI2 include a connect function for
establishing connections.
• Connection Object: The connect function returns a Connection object, enabling database
interaction.
• Query Execution: Use the execute method on the Connection object to run queries.
• Popular Python ORM: SQLAlchemy is widely used and was the first
ORM ported to Python 3.
SQLAlchemy
• SQLAlchemy provides abstraction over database APIs, enabling code to
work across multiple database systems.
• Ease of Transition: Start with SQLite as a backend and later port to other
databases like MySQL with minimal changes.
Connecting to a database
Ensures that all the tables associated with that
Base class exist. It also issue some sort of CREATE
TABLE call.
SQLAlchemy: Adding and querying objects
• Do it as an assignment
References
• Phillips, Dusty. Python 3 object oriented programming. Packt
Publishing Ltd, 2010.
• Lutz, Mark. Learning python: Powerful object-oriented programming.
" O'Reilly Media, Inc.", 2013.
QUESTIONS
?