0% found this document useful (0 votes)
14 views

Lecture6 (1)

Lecture 7 focuses on database application programming using SQL and Python, discussing various programming techniques for database access. It outlines the general workflow for setting up a database and writing a Python application to interact with it, including connection management and executing SQL queries. Key concepts include the use of the MySQL Connector API and handling transactions with commit and rollback operations.

Uploaded by

trol.man890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture6 (1)

Lecture 7 focuses on database application programming using SQL and Python, discussing various programming techniques for database access. It outlines the general workflow for setting up a database and writing a Python application to interact with it, including connection management and executing SQL queries. Key concepts include the use of the MySQL Connector API and handling transactions with commit and rollback operations.

Uploaded by

trol.man890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture 7.

Database Application
programing with SQL, Python
[email protected]
Connection to previous lectures
Client
Normalized Application
Relational Relational Uses
Model SQL queries
ER/EER Model Database
Lecture 3 ER and EER Lecture 4 Relation Lecture 5 Lecture 6 Lecture 7 Application
Conceptual models Model SQL Programming

•High –level data •Medium-level data •Low-level •Functional •Write Python


abstraction using abstraction using abstraction Dependencies program to
Entity Relation Relational Models •SQL standard •Normalization: manipulate with
(ER) and Enhanced •From ER to language to create 1NF, 2NF,3 NF Relational
Entity Relation Relational Model and manipulate •Relational Algebra Database using
(EER) models the database Operations SQL

A program written in
Diagram Software MySQL Server, any programming
(e.g., diagrams.net ) Any DBMS (e.g., MySQL Workbench) language: Python, Java,
JavaScript, etc..
2(28)
Outline
• Database Programming Techniques and Issues (Chapter 10)
• Python Database Application Programming (see
References)

3(28)
Introduction to SQL Programming Techniques
• Most database access is accomplished through software programs
that implement database applications.
• This programs usually developed in different programming languages:
Java, C#, Python, C/C++, JavaScript, PHP
• There are whole books devoted to each database programming
technique. New techniques are developed all the time, and updates
for existing techniques are also evolving.
• Although there are SQL standards exists which are continually
evolving and each DMBS may have some variations from the
standard.
4(28)
Overview of main approaches for accessing a
database from programs
• Approach 1. Embedding database commands in a programming
language. The database statements are marked with special syntax
which is recognized by the precompiler or preprocessor.
• Approach 2. Using a library of database functions. This is the most
common approach to access the database though library written for
specific programming language (Python, Java, PhP, etc.). It provides
ready functions to connect to a database, prepare and execute
queries. Nowadays this approach is known as application
programming interface (API).
• Approach 3. Designing a new database programming language.

5(28)
Approach 2 Issues
• Data type mismatch: data types of the programming language (e.g.,
Python, Java, etc.) differ from the attribute types available in SQL DBMS.
• Consequences: program crashed, or SQL transaction (query) is failed.
• Solution: a binding between attribute types (SQL) and compatible data type of
programming language have to be performed. The values sets should be also check
in the program side before performing the parsing data or data convertation,
especially for dates data type.
• Data structure: the data structure which comes from SQL is set of tuples,
like this [(‘att1’, ‘att2’, ‘att3’,..),(),(),()…]. In order to access the individual
tuple’s elements, you need a binding to map query result data structure to
an appropriate data structure in the programming language.
• Solution: A general function is needed to loop over the tuples in a query result and
re-structure the data in more convenient data structure (e.g., dictionaries in Python).
Also you need to have another function which maps the programming language
structure to set of tuples (SQL) for updating/inserting operations to database.

6(28)
General Programming Workflow
Setting up database:
• Run MySQL Server
• Use any DBMS to connect to MySQL server
• Create a database using DBMS interactive interface
• Define database schema using DBMS interactive interface
Writing Database Application/Program:
• Establish the connection to MySQL Server and database. In this step, we specify the URL
address (e.g., localhost) of the machine where the database server is located, and providing a
login account name and password for database access. Note! In real life application we do not
use an admin (root) user to access the database. The database admin have to create a new user
with read and write access for each database application/program. This user must not have
access to modify the database schemes (such as delete tables, adding new tables)
• Once the database connection is established, the program can interact with the database by
submitting queries commands.
• When the program no need anymore access to the database, it should terminate or close the
connection to the database.

7(28)
Overview of Python
Database Programming

8(28)
Python Database 2-tier Architecture

Interacts Python Program DB-API MySQL Server

Database
User

1-tier 2-tier

9(28)
DB-API for Python
• The Python DB API is a widely used module that provides a database
application programming interface
• Python DB-API is independent of any database engine, which enables you
to write Python scripts to access any database engine
• The Python DB API implementation for MySQL is MySQLdb
• MySQLdb version 1 is not supported anymore
• MySQLdb version 2 is outdated and migrated to so called mysqlclient
library
• There are other DB-API exists: Benchmarking MySQL drivers (Python 3.4)
among them is MySQL Connector API which we are going to use in this
course

10(28)
MySQL Connector API
• MySQL provides MySQL adapter called MySQL connector API (written
in Python and does not require any third-party libary) which enables
Python programs to access MySQL databases.
• This adapter allows the conversion between Python and MySQL data
types
• It includes:
• Connection
• Cursor for querying the data
• Handling exceptions

11(28)
Database Application workflow of Python
program
• 1 Connect to the MySQL server
• 2 Connect to an existing database or create a new database
• 3 Write a Python interactive application with some user input
• 4 Execute a SQL query and fetch results
• 5 Print the results to end-user
• 6 Close the connection to the MySQL server

12(28)
Connection to Database Server (MySQL Server)

• The getpass module is used to hide the password


• The connect object from mysql.connector module
is used to establish the database connection
• try and catch statement is used to catch and print
exceptions/errors to the database
• The with statement is used to taking care of
closing the database connection. Leaving unused
open connections can lead to several unexpected
errors and performance issues
• The input is used for providing the user and
password in order to avoid the hardcoded login
credentials. More secure ways to store sensitive
information is using environment variables

13(28)
Create a new database (1)
Use a cursor to execute a SQL query in Python.
A cursor is a a mechanism that enables traversal over the records in a database (similar to Iterator concept)
MySQL Connector/Python provides the MySQLCursor class, which instantiates objects that can execute MySQL queries
in Python.
An instance of the MySQLCursor class is also called a cursor.

SQL Python
CREATE DATABASE books_db; db_query = “CREATE DATABASE books_db”
with connection.cursor() as cursor:
cursor.execute(db_query )

Note! In MySQL, it’s mandatory to put a semicolon (;) at the end of a statement, which denotes the termination of a query.
However, MySQL Connector/Python automatically appends a semicolon at the end of your queries, so there’s no need
to use it in your Python code.

14(28)
Create a new database (2)
• You might receive an error here if a database with the same name
already exists in your server.
• To check if the database is already exists use the following code:

print(database)

15(28)
Connecting to an Existing Databases
• In most of the cases we already have
created database using the DBMS
and we only want to connect to it
from our Python application
• We can do this the same connect()
function by adding additional
parameter called database, which is
the name of database.

16(28)
Inserting Records in Tables (1)
• Using cursor.execute() method for
inserting small number of records and
the records can be hard-coded
• Use connection.commit() method
to perform changes in the actual table.
• You can use many times the
cursor.execute() method and in the end
use once connection.commit() method
to perform atomic transaction.
• Note! You don’t need to add data for IDs
as the AUTO_INCREMENT build in
function automatically creates id values
in database.
17(28)
Inserting Records in Tables (2)
• Using cursor.executemany()
method when the data are stored in
file or generated by a different script.
• It takes two parameters:
• A query string
• A list with records to be inserted
• The code uses %s as a placeholder for
the two strings that had to be inserted
in the query string
• Placeholders act as format specifiers
and help reserve a spot for a variable
inside a string. The specified variable is
then added to this spot during
execution.

18(28)
Inserting Records in Tables (3)

Note! We use sting placeholder %s for


any data type

Note! The Python program should check if the data


type of the values are correct for the database

19(28)
Reading Records From the Database (1)
• Create SELECT query string
• Use cursor.fetchall() method to extract
the records
• It returns a list of tuples representing
individual records from the table
• Tuple is ordered collection of objects, they
are defined by enclosing the elements in
parentheses () instead of square brackets.
And they are immutable (read-only).
• Use the LIMIT clause with (offset,max) to
constrain the number of rows that are LIMIT 5 means only the first 5 records are fetched
received from the SELECT statement. And to
provide the pagination when handling large limit = 5
volumes of data.
“””…. LIMIT {0},{1};”””.format(offset,limit)
…..
If button_next_click:
offset + =1 //increase the offset 20(28)
Reading Records From the Database (2)

21(28)
Printing Tuples list as Table in Python
Functions to map data structure from database structure
to Python data structure and vise versa
Printing tuples as table

22(28)
Filtering Results Using the WHERE Clause

23(28)
Updating Records From the Database

Note: In the UPDATE query, the WHERE clause helps specify


the records that need to be updated. If you don’t use WHERE,
then all records will be updated!

24(28)
Deleting Records From the Database

Note: Deleting is an irreversible process. If you don’t use the WHERE clause, then
all records from the specified table will be deleted. You’ll need to run the INSERT
INTO query again to get back the deleted records.

25(28)
Commit & Rollback Operation to manage
transactions in Python
Transaction
succeeded
All Queries commit()
Transaction
• The commit() method is used to make executed
sure the changes made to the database successfully
{Query 1,
are consistent Query2,
• Syntax: connection.commit() Query
• The rollback() method is used to revert Initiate 3,…}
the last changes made to the database transaction
rollback()
when one of the query failed. Any of the
Query failed
• Syntax: connection.rollback()

Transaction
failed

26(28)
Example

Do not hard code the user credentials!


You can do it only in development mode

27(28)
References
Python and MySQL Database: A Practical Introduction: Web URL
Use Commit and Rollback to Manage MySQL Transactions in Python:
https://pynative.com/python-mysql-transaction-management-using-
commit-rollback/

28(28)

You might also like