1. Using SQL¶
1.1. Surfer Notes¶
The Surf Federation has the following data: surfer’s number, name, country, average grade, style, age.
How to use this information?
For example, I want to know all surfers over 25.
Using a database! In this case, SQL.
Previously, a database was created with a table called
surfers
.This table has the information of the surf tournament participants organized in the following columns:
id
,name
,country
,average
,style
,age
.id
is an integer that identifies the contestant.name
is a string with his/her name and nickname (if any).country
shows the country he/she is representing in the tournament.average
is the average score obtained in the tournament.style
is the style of his/her surfboard.age
is an integer that shows the contestant’s age.
In this exercise we will read the data from the table surfers
and display
only those over 25 with an SQL statement (known as a query).
Currently, the query returns all the data in the surfers
table.
xxxxxxxxxx
# the connection method of sqlite3 has been imported for use
# one can do this by the statement
from sqlite3 import connect
# initiate the connection and connect to the predefined surfers table
database = connect('surfers_en.db')
# create a cursor and execute the query
cursor = database.cursor()
query = 'SELECT * FROM surfers'
cursor.execute(query)
# fetch the data
rows = cursor.fetchall()
# print the data
if(rows!=None):
for row in rows:
print("ID :",row['id'])
print("Name :",row['name'])
print("Country :",row['country'])
print("Average :",row['media'])
print("Style :",row['style'])
print("Age :",row['age'])
Activity: 1.1.1 ActiveCode (ac_42_1_en)