Introduction to programming using Python

Session 10

Matthieu Choplin

http://mattchoplin.com/

Objectives

  • To handle exceptions using the try/except/finally clauses
  • To raise exceptions using the raise statements
  • To become familiar with Python’s built-in exception classes
  • To access exception object in the handler
  • Introduction to Database Connection

Exception Handling

What happens if the user enters a file or an URL that does not exist? The program would be aborted and raises an error. For example, if you run count_letter.py with an incorrect input:

c:\session10\python count_letter.py
Enter a filename: non_existant_file.txt
Traceback (most recent call last):
  File "path_to/count_letter.py", line 18, in  <module>
    main()
  File "path_to/count_letter.py", line 7, in main
    f = open(filename)
FileNotFoundError: [Errno 2]
No such file or directory: 'non_existant_file.txt'

Process finished with exit code 1

The try ... except clause

Catching one exception type

try:
 <body>
except <ExceptionType>:
 <handler>

The try ... except clause

Catching several exception types

try:
 <body>
except <ExceptionType>:
 <handler1>
 <handler1>
...
except <ExceptionTypeN>:
 <handlerN>
except:
 <handlerExcept>
else:
<process_else> # will be executed if not exception
finally:
<process_finally> # executed with or without exception

Example

def main():
    try:
        numbers = input("Enter two integers,"
                "separated by a comma: ").split(',')
        number1 = int(numbers[0])
        number2 = int(numbers[1])
        result = number1 / number2
        print("Result is " + str(result))
    except ZeroDivisionError:
        print("Division by zero!")
    except SyntaxError:
        print("A comma may be missing in the input")
    except:
        print("Something wrong in the input")
    else:
        print("No exceptions")
    finally:
        print("The finally clause is executed")
main()

Raising Exceptions

You learned how to write the code to handle exceptions in the preceding section. Where does an exception come from? How is an exception created? Exceptions are objects and objects are created from classes. An exception is raised from a function. When a function detects an error, it can create an object of an appropriate exception class and raise the object, using the following syntax:

raise ExceptionClass("Something is wrong")

Creating our own Exception

class ExceptionClass(Exception):
    pass

raise ExceptionClass('Something is wrong')

Exercise: raising our own exception

Ask the user to enter a name, if the name is 'admin', raise an exception of type CustomException with the message 'Hep! You are not admin'

Processing Exceptions Using Exception Objects

You can access the exception object in the except clause with the as keyword.

try:
    number = int(input("Enter a number: "))
    print("The number entered is", number)
except ValueError as ex:
    print("Exception:", ex)

Using the with statement

It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:

with open('Python_projects.txt', 'r') as f:
    read_data = f.read()

assert f.closed

Introduction to SQL Database Connection (1)

  • You will use databases when you want to structure your data using tables and fields and persist them in memory.
  • The data stored in database will remain even if you close your program.
  • You can visualize what a database is by comparing it to a spreadsheet where the file will be the table, the header of the rows will be the fields or columns and the rest of the rows will be the data stored in this table
  • The simplest version of SQL is SQLite, we can interact with it through the module sqlite3

Introduction to SQL Database Connection (2)

  • We need to import the module and connect to a database (that will be created if it does not exist).
  • import sqlite3
    
    connection = sqlite3.connect("test_database.db")

NB: the data created will be stored in the file test_database.db that is actually the database

Introduction to SQL Database Connection (3)

We then need a cursor to execute commands on the database

import sqlite3

connection = sqlite3.connect("test_database.db")
cursor = connection.cursor()
# We create our first TABLE People that will
# store the field FirstName, LastName and Age
cursor.execute(
    "CREATE TABLE People("
    "FirstName TEXT, "
    "LastName TEXT, "
    "AGE INT)")

Introduction to SQL Database Connection (4)

Imagine that the TABLE we have created is like a spreadsheet file ready to take data

It means that we can now insert data into this table with the "INSERT" command

cursor.execute("INSERT INTO People "
               "VALUES ('Ron', 'Obvious', 42)")
# we have to commit to actually
# save the record in database
connection.commit()

Introduction to SQL Database Connection (5)

When working with database, it is a good idea to use the with keyword to simplify your code, similar to how we used the with to open files

with sqlite3.connect("test_database.db") as connection:
	# perform any SQL operation

Also, you will no longer need to use the commit() explicitly

Introduction to SQL Database Connection (6)

Imagine that you want to concatenate a string with a SQL command

Do not do this:

first_name, last_name, age = 'John', 'Doe', 21
with sqlite3.connect("test_database.db") as connection:
    cursor = connection.cursor()
    cursor.execute(
        "INSERT INTO People VALUES"
        "('"+ first_name + "', '" + last_name + "', " + str(age) + ")")

Introduction to SQL Database Connection (7)

The database is correctly updated, you can check that with the following command

with sqlite3.connect("test_database.db") as connection:
    cursor = connection.cursor()
	cursor.execute("SELECT * FROM People")
    rows = cursor.fetchall()
    print(rows)

Introduction to SQL Database Connection (8)

Using the same method as 2 slides before, what happen if we try to add a user with the LastName "O'Connor"?

first_name, last_name, age = 'John', 'O\'Connor', 21
with sqlite3.connect("test_database.db") as connection:
    cursor = connection.cursor()
    cursor.execute(
        "INSERT INTO People VALUES"
        "('"+ first_name + "', '" + last_name + "', " + str(age) + ")")

We will get an error because the "'"

Introduction to SQL Database Connection (9)

Introduction to SQL Database Connection (10)

To avoid SQL injection, use the following instead:

first_name, last_name, age = 'John', 'O\'Connor', 21
with sqlite3.connect("test_database.db") as connection:
    cursor = connection.cursor()
    cursor.execute(
        "INSERT INTO People VALUES"
        "(?, ?, ?)", (first_name, last_name, age))
    cursor.execute("SELECT * FROM People")
    rows = cursor.fetchall()
    print(rows)

Introduction to SQL Database Connection (11)

The question marks act as a placeholder for the (first_name, last_name, age) tuple; this is called a parameterized statement. You should always used parameterized SQL statement

File used for the example: test_db.py

Exercise

  • Populate the database with additional records
  • Display the People who are older than 18 using a select command and a cr.fetchall()