0% found this document useful (0 votes)
5 views3 pages

DB TP1 Enonce

This document provides a guide on interacting with a local SQLite database using Python's sqlite3 module. It outlines the steps to connect to the database, execute SQL queries, and retrieve results, along with examples of simple queries, joins, computations, groupings, set operators, and self joins. Additionally, it includes hints for specific queries related to a movie database schema.

Uploaded by

c
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)
5 views3 pages

DB TP1 Enonce

This document provides a guide on interacting with a local SQLite database using Python's sqlite3 module. It outlines the steps to connect to the database, execute SQL queries, and retrieve results, along with examples of simple queries, joins, computations, groupings, set operators, and self joins. Additionally, it includes hints for specific queries related to a movie database schema.

Uploaded by

c
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/ 3

Databases : interacting with local database using Python

1 The sqlite3 module


In this practice class we’ll go over what we’ve seen last year for SQL queries, but unlike last year, we won’t be
using a dedicated software to open the database, instead we are going to be using Python. In particular we will be
using a module called sqlite3. That means your .py file will start with : import sqlite3.

Its working is simalar to the way we can interact with .txt or .csv files. First we need to open a connection to
the database :

db = sqlite3.connect("movies.sqlite")

In this instruction db is an arbitrary name for the variable in which we store the connection, and connect is a
method (likely a static method) from the sqlite3 module that returns an instance of the Connection class.

Then, to move through the database we are going to declare a cursor. The cursor will serve to execute the queries :

c = db.cursor()

Once again, c is an arbitrary name for the variable storing the cursor, and cursor is a method from the Connection
class returning an instance of the Cursor class.

To execute queries, we use the execute method of the Cursor class. The query is a string given as the single
parameter :

query0 = c.execute("SELECT * FROM Artists")

Beware ! The execute method return an type of object called an iterable (the same as a stream returned by the
open function), which is not printable as is. You need to use the fetchall (or fetchone) method to get the result.
fetchall returns a list of tuples :

res0 = query0.fetchall()

In this example res0 is a list and we can call it in the console to see its content. Or, for a more orderly display we
can define a function that prints out the items in a list one by one :

def displayList(l,n = None):


if n is None :
n = len(l)
print("number of results : " + str(len(l)))
for i in range(n):
print(l[i])

1
Informatique — CPES2 Databases TP1

2 Queries
2.1 Presenting the database
For this practice class you will be working with the following database (the movie titles and genre are still in french
but all the attribute names are in english) :

Countries : Users : Artists : Movies :


- id - email - id
- id

- name - lastname - title


- lastname
- language - firstname - year_of_release

- firstname
- country (Countries.id) - director_id (Artists.id)

- year_of_birth - genre
- password

- summary

- country_id (Countries.id)

Reviews : Roles :
- movie_id (Movies.id) - movie_id (Movies.id)

- email (Users.email) - actor_id (Artists.id)

- score - role_name
Relational diagram

In your class directory you’ll find a file named movies.sqlite. Make sure that you copy it in the same directory
as the .py you’ll be using today.

2.2 Questions
2.2.1 Simple queries
1. List the names of the artists born between 1953 and 1967 included.
Hint : 67 lines.

2. List the titles of movies whose genre is ’Fantastique’ or ’Drame’.


Hint : 16 lines.

2.2.2 Joins
3. Give the country in which ’Le bon, la brute et le truand’ has been filmed.
Hint : It was filmed in Italy.

4. List (without repetition) the lastnames of directors havinf filmed at least one movie in ’France’, in alphabetical
order.
Hint : 9 lines.

Lycée Chateaubriand 2/3


Informatique — CPES2 Databases TP1

2.2.3 Computations
5. Give the number of movie genre.
Hint : There are 12.

6. Give the number of ’Science-fiction’ movies.


Hint : There are 8.

7. Give the minimal score, maximal score, and average score received by ’Science-fiction’ movies.
Hint : It’s 2, 5 and 3.

2.2.4 Groupings
8. Give for each movie its average, minimal, and maximal scores.
Hint : 38 lines, among which American Beauty with the scores 4, 5, 4.5.

9. Give the number of movies for each genre.


Hint : there are 14 movies whose genre is ’Drame’.

10. List the emails of the users who reviewed at least 15 movies.
Hint : 2 lines.

11. List the most common first names among artists born before 1940.
Hint : 52 lines, Robert is the most common one.

12. Give the lastname of the actor who starred in the most movies (we will assume there is only one).
Hint : It’s Bruce Willis.

2.2.5 Set operators


13. List the lastnames of directors who directed at least one ’Thriller’ movie and at least one ’Horreur’ movie.
Hint : 2 lines.

14. List the lastnames of actors who starred in movies in the 90’s, but not in the 80’s.
Hint : 57 lines.

15. List the titles of movies who received a score of 1 or 5.


Hint : 14 lines.

2.2.6 Self joins


16. List without repetition the pairs of distinct movies released the same year, with the year.
Hint : 73 lines.

Lycée Chateaubriand 3/3

You might also like