DB TP1 Enonce
DB TP1 Enonce
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 :
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 :
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) :
- 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)
- 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.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.
2.2.3 Computations
5. Give the number of movie genre.
Hint : There are 12.
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.
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.
14. List the lastnames of actors who starred in movies in the 90’s, but not in the 80’s.
Hint : 57 lines.