In the previous exercise we were able to import the ‘sqlite3’ library and use that to open our SQLite database — so far so good! But we still haven’t retrieved any information from it. Since we have access to our database as a JavaScript object, let’s try running a query on it. Recall that a query is a statement that speaks to a database and requests specific information from it. To execute a query and retrieve all rows returned, we use db.all()
, like so:
db.all("SELECT * FROM Dog WHERE breed='Corgi'", (error, rows) => { printQueryResults(rows); });
In the previous example, we used the db.all()
method to retrieve every dog of breed “Corgi” from a table named Dog and print them.
Instructions
Open a call to db.all()
. Inside, add a query that will select all the rows from the TemperatureData
table. For now, you can leave the callback empty.
Create your callback function as the second argument of db.all()
. It should take two arguments and print the second with the printQueryResults()
function imported at the top of your file;
Replace your query with a new query that will only SELECT
the rows in the TemperatureData table with the year 1970.