24.9. Exercises: Databases¶
In these exercises, you will perform several CRUD operations with a new database file.
24.9.1. Part A: Set Up a New Database¶
Launch Visual Studio Code and use the File menu to open your
database_practice
directory.Double-click the
.gitignore
file to open it in the editor.Choose a name for a new database file (like
exercises.db
) and enter it on its own line in.gitignore
.Create an
exercises.py
file. Start by importing thesqlite3
module, then open a connection to your new database.Don’t forget to define a
cursor
object.Run the program. Your new database should appear in the file tree.
The setup is done. You are ready to go!
24.9.2. Part B: Add a Table¶
Imagine that you want to apply your new database skills to bring some order to
your game closet. To do this, you will use a games
table to store some
information. The table needs at least 5 columns:
game_id
: This is the primary key assigned to each row in thegames
table.title
: This is the name of the game.num_players
: Gives a range for the number of people required to play the game.min_age
: Identifies the youngest recommended player age for the game.ranking
: Assigns a value of 1 - 5 to the game. The higher the number, the more you like the game.
The title
and num_players
columns will hold TEXT
data types.
game_id
and min_age
will be integers. ranking
will be a FLOAT
data type.
Define a
sql_query
variable and assign it a string to create thegames
table. Be sure to include thePRIMARY KEY
syntax for thegame_id
column.Use
cursor.execute()
to run the SQL query and create the new table.Verify that the table was created by expanding the database name in the SQLITE EXPLORER tab.
24.9.3. Part C: Add Rows¶
Assign a new string to your
sql_query
variable. It should use the INSERT INTO keywords to add an entry to thegames
table. Remember to use placeholders after theVALUES
keyword.For the first new row, set the
title
as “Qwirkle”, thenum_players
as “2 - 4”, themin_age
as 6, and the ranking as 4.5.You do NOT need to include a value for
game_id
, since thesqlite3
module automatically assigns primary key values.Reuse the
sql_query
string to add another entry to thegames
table. This time, choose your own values for the columns.It is possible to fill in only SOME of the columns for a given row. To skip one or more columns, leave their names and values out of the SQL command.
Add 3 - 5 more rows to the
games
table. At least one of these should NOT fill in all of the columns.Finish off your code with a
.commit()
statement, then.close()
the connection to the database.Run your program, then use the Play button in the SQLITE EXPLORER tab to view the
games
table.
Note
Notice that NULL
appears in a cell whenever a value for that column
is not assigned.
24.9.4. Part R: Access Game Data¶
For this section, you will need to use several different query strings to complete the steps.
Use SELECT … FROM … to return results from all of the columns in the
games
table.Use
list()
to convert the returned results, then print the data to the console in a clear fashion. Tip: Define a function to accomplish this.Repeat step 1, but return and print a single entry based on its
game_id
value. You will need to include theWHERE
keyword in your SQL query.Repeat step 1, but print only the
title
data from the table. Bonus: Display the games in alphabetical order.Select the
title
andmin_age
data, but print the results in DECREASING order by age.
24.9.5. Part U: Modify Game Data¶
Remember to include a WHERE
clause in your UPDATE
queries! If you leave it out, then ALL rows in the table will be changed.
Don’t forget to view the games
table after each step to confirm your work.
Update a single record based on its
game_id
.The first entry we added in Part C has a
game_id
of1
. UseUPDATE ... SET ... WHERE
to change theranking
for this entry to a different value.Use a single
UPDATE
statement to change two columns for the entry withgame_id
= 4.
Use ALTER TABLE to add a new column, called
will_donate
, to the table. Set the data type toBOOLEAN
.With a single
UPDATE
command, set thewill_donate
value toTrue
for all entries that have aranking
lower than 3.0.Note
SQLite converts boolean values to integers.
True
is stored as1
, whileFalse
is saved as0
.
24.9.6. Part D: Remove Games¶
Remember, there is no undo option after running DELETE!
Delete a single row from the table based on its
game_id
value.Write a single
DELETE
command to remove any games from the table that you don’t consider fun. You can use either theranking
orwill_donate
value to decide which rows to remove.
24.9.7. Bonus Exercises¶
Whew! You made it through all the exercises. Nice work!
Take a quick break and, if you wish, try these additional tasks that go above and beyond the basic SQL commands.
Use logical operators (
AND
,OR
,NOT
) inWHERE
statements.Print the complete records for the games, but only if the
min_age
columnIS NOT null
.Experiment with the ORDER BY keywords.