Read

The SELECT command is used to read data from our database. Start by just printing all the data in the temperature table (currently, one row):

    SELECT * FROM temperature;

This will give you an output of this:

    2017-06-18 12:13:50|16.7
Your createdAt value will differ based on when you ran the INSERT statement.

The values of each row are separated by the | character.

Run a few more dummy inserts so that we can see the filtering capabilities of SQLite:

   INSERT INTO temperature VALUES (datetime('now'), 16.9);
INSERT INTO temperature VALUES (datetime('now', '+1
second'
), 14.7);
INSERT INTO temperature VALUES (datetime('now', '+2
second'
), 22);
INSERT INTO temperature VALUES (datetime('now', '+3
second'
), 21.1);

Your temperature table should now look similar to this:

Using SQL, you can filter the rows and columns that you want to pick from your data.

To filter columns, you explicitly mention the one you want to pick instead of the wildcard (*):

    SELECT "value" from temperature;

This statement prints only the value column from our temperature table:

     16.7
16.9
14.7
22.0
21.1

To filter rows, use the WHERE directive along with the filter condition:

      SELECT * FROM temperature WHERE createdAt > "2017-06-18
12:26:09"
;

With this statement, only the rows where the createdAt date is after 2017-06-19 10:52:50 are displayed:

     2017-06-18 12:26:10|22.0
2017-06-18 12:26:13|21.1

We can combine both of these types of filters as well:

     SELECT "value" FROM temperature WHERE createdAt > "2017- 
06-18 12:26:09"
;

This will display all values from the temperature table whose readings were taken after 2017-06-19 10:52:50:

    22.0
21.1

As you can probably tell, filters are going to be a very important application in fetching the correct data for us, and we are going to deal with them quite a lot going forward.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.2.122